我正在我的 Android 应用程序中执行下载功能。我在这里下载 .pptx
。这是我的代码。
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(DetailSeminarActivity.this, "Downloading slide", Toast.LENGTH_SHORT).show();
try {
/*
* Intent myIntent = new Intent(Intent.ACTION_VIEW,
* Uri.parse(GdocLink)); startActivity(myIntent);
*/
Uri uri = Uri.parse(downloadSlidesLink);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(browserIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(DetailSeminarActivity.this,
"No application can handle this request, Please install a webbrowser",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Intent myIntent = new Intent(arg0.getContext(), SeminarActivity.class);
startActivityForResult(myIntent, 0);
}
});
我以前的设备可以下载文档(Samsung S3),但是我的新设备 OnePlus one 不能下载它。它跳到:
"No application can handle this request, Please install a webbrowser"
我尝试从 gmail 下载并打开相同的 .pptx
文档,它成功了。我该如何运行?
提前致谢。
最佳答案
您明确地将 com.android.browser
设置为处理 Intent 的组件。它是旧版 Android 库存浏览器的包名,在最新的设备中不再安装。由于这个原因,您的代码在 Samsung S3 上有效,但在 One Plus One 上不起作用。一个快速的解决方法是删除这些行:
browserIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
当您按下按钮时,如果您有多个应用程序可以处理 Intent
,则 IntentChooser
将打开,否则应用程序将直接打开。
无论如何,我建议使用 DownloadManager处理文件的下载:
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uriString));
downloadManager.enqueue(request);
您还可以注册接收器以了解下载何时完成:
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//Your code
}
}
};
关于android - 无法在 Android 应用程序中继续下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643786/