无法在 android 个应用中继续下载
Cannot proceed download in android app
我正在 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
设置为处理意图的组件。它是旧 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 应用程序中执行下载功能。我在这里下载 .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
设置为处理意图的组件。它是旧 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
}
}
};