我正在编写一个代码,允许从服务器下载应用程序并自动打开它,但是在代码的最后一步调用Intent(安装)后面临异常
例外:
android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.VIEW dat = file:/// file:/storage/emulated/0/Download/application.apk typ = application / apk}
请知道有问题的文件是一个名为application.apk的应用程序,我想在下载完成后直接打开安装页面。以下是代码:
private void download(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
final String filename= URLUtil.guessFileName(url, null, null);
final String file_extension = filename.replace("bin","apk");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file_extension);
final DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
final Long enq= dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
/// OPEN File
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enq);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
//TODO : Use this local uri and launch intent to open file
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(uriString)), "application/apk");
webview_java.this.startActivity(install);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
我也尝试过startActivity而不是调用webview_java.this.startActivity但它会抛出同样的错误