Android:使用可下载媒体按钮打开 URL,但不打开浏览器窗口。如何?

发布于 2024-10-09 13:10:00 字数 913 浏览 1 评论 0原文

是否可以在不让浏览器打开链接的情况下打开 URL?

我已经遵循了一些教程,但无法管理此任务

我打开的 url 是媒体内容 url (.dd),因此我不需要让浏览器显示任何内容,只需访问该 url 即可触发下载。

下面是在浏览器窗口中打开 URL 然后执行下载的代码,但是如何在不打开浏览器窗口的情况下打开 URL,几乎就像在后台打开一样?

希望有人能告诉我怎么做?

谢谢你,露西

Button openURLLButton = (Button) findViewById(R.id.save); // Retrieve the button from the XML file
    openURLButton.setOnClickListener(new View.OnClickListener() {  //Add a listener for when the button is pressed
        public void onClick(View v) {
            openUrl();          
        }
    });
}

protected void openUrl() {
    String url = "http://domain.com/download.dd"; 
    Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
    i.setData(Uri.parse(url));  // Add the url data (allowing android to realise you want to open the browser)
    startActivity(i); // Go go go!
}

}

Is it possible to open a URL without having the browser open up the link?

I have followed a few tutorials but cannot manage this task

The url i'am opening is a media content url (.dd) so i do not need to have the browser display anything, only access the url to then trigger the download.

Here is the code which opens the url in a browser window then performs the download, but how do i open the URL without opening a browser window, almost like it is opening in the background?

Hope someone can show me how?

Thankyou, Lucy

Button openURLLButton = (Button) findViewById(R.id.save); // Retrieve the button from the XML file
    openURLButton.setOnClickListener(new View.OnClickListener() {  //Add a listener for when the button is pressed
        public void onClick(View v) {
            openUrl();          
        }
    });
}

protected void openUrl() {
    String url = "http://domain.com/download.dd"; 
    Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
    i.setData(Uri.parse(url));  // Add the url data (allowing android to realise you want to open the browser)
    startActivity(i); // Go go go!
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2024-10-16 13:10:00
protected InputStream openUrl() {
    String url = "your url";
    InputStream stream;
    HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
    if (connection.ResponseCode == HttpURLConnection.HTTP_OK) {
        stream = connection.getInputStream();
    }
    return stream;
}

上面的代码(我没有包含所需的异常处理)将获取作为 InputStream 的 url。然后取决于您想如何处理数据。 InputStream 基本上是一个字节数组。我不熟悉 *.dd 作为文件类型,但是一旦有了数据流,您就可以使用其他 InputStreamOutputStream 子类来操作它,例如FileOutputStream 将数据写入文件。

编辑:

如果您想更改壁纸,请查看 sdk 参考中的 WallpaperManager 。它允许您从 DrawableInputStream 设置壁纸。

protected InputStream openUrl() {
    String url = "your url";
    InputStream stream;
    HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
    if (connection.ResponseCode == HttpURLConnection.HTTP_OK) {
        stream = connection.getInputStream();
    }
    return stream;
}

The above code (I haven't included the required exception handling) would get the url as an InputStream. It then depends on what you want to do with the data. The InputStream is basically an array of bytes. I am not familiar with *.dd as a file type but once you have the stream of data you can then use other InputStream or OutputStream subclasses, to manipulate it, such as FileOutputStream to write the data to a file.

Edit:

If you wish to alter wallpapers then look at WallpaperManager in the sdk reference. It allows you to set a wallpaper from a Drawable and also an InputStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文