Android:使用可下载媒体按钮打开 URL,但不打开浏览器窗口。如何?
是否可以在不让浏览器打开链接的情况下打开 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码(我没有包含所需的异常处理)将获取作为
InputStream
的 url。然后取决于您想如何处理数据。InputStream
基本上是一个字节数组。我不熟悉 *.dd 作为文件类型,但是一旦有了数据流,您就可以使用其他InputStream
或OutputStream
子类来操作它,例如FileOutputStream
将数据写入文件。编辑:
如果您想更改壁纸,请查看 sdk 参考中的
WallpaperManager
。它允许您从Drawable
和InputStream
设置壁纸。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. TheInputStream
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 otherInputStream
orOutputStream
subclasses, to manipulate it, such asFileOutputStream
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 aDrawable
and also anInputStream
.