我正在我的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无法下载。它跳到:
"No application can handle this request, Please install a webbrowser"
我尝试从gmail下载并打开相同的.pptx
doc,但它确实有效。我怎么能跑这个?
提前致谢。
答案 0 :(得分:1)
您明确将com.android.browser
设置为处理意图的组件。它是旧版Android股票浏览器的软件包名称,不再安装在最新的设备中。出于这个原因,在三星S3上你的代码可以运行,但它不适用于One Plus One。快速修复可能是删除这些行:
browserIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
当您按下按钮时,如果您有多个可以处理IntentChooser
的应用,则会打开Intent
,否则该应用会直接打开。
无论如何,我建议使用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
}
}
};