处理我的应用程序中的下载意图 [英] Handle download intents in my app
问题描述
我的webview应用程序已经可以处理外部URL的罚款(打开来自外部应用程序(如viber,浏览器等)的链接),就像这样-(
My webview app already handles external URL's fine (opens links from external apps like viber, browsers, etc) like this- (i got this code from here)
// get URL from browsable intent filter
TextView uri = (TextView) findViewById(R.id.urlField);
// get the url
url = getIntent().getDataString();
Uri data = getIntent().getData();
if (data == null) {
uri.setText("");
} else {
uri.setText(getIntent().getData().toString());
fadeout();
}
// }
在我的网络视图设置中
// Load URL from Browsable intent filter if there is a valid URL pasted
if (uri.length() > 0)
webView.loadUrl(url);
else
// dont load
这是我在网络视图中处理下载的方式
this is how i handle downloads within my webview
// download manager
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
// download manager
但是,我也想处理其他应用程序传递的下载意图.我该怎么办?
However, I'd like to handle that download intents passed by other apps as well. How do i do that?
这是我用来从另一个应用程序打开系统默认应用程序选择器的功能.(我的应用程序在此处列出)-
This is what i use to open the system default app chooser from another app.(my app is listed here)-
// download via...
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Toast.makeText(getApplicationContext(), "don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",
Toast.LENGTH_SHORT).show();
}
});
// download via..
每当我单击下载链接时,我的应用程序即会打开,但会停滞不前而不是打开URL.我希望它像一个下载器应用一样
Whenever i click a download link, my app opens, but just sits still instead of opening the url. I want it to act like a downloader app
推荐答案
您需要在活动中放入以下代码,以响应意图,
You need to put below code in your activity which response the intent,
void onCreate(Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_VIEW.equals(action))
{
*/if (type.startsWith("image/"))
{*/
downloadImage(intent); // Download image being sent
}
}
void downloadImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Write Your Download Code Here
}
}
这篇关于处理我的应用程序中的下载意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!