我要在下载完成的应用程序时自动安装应用程序,我正在使用sdk> 24,功能下载成功完成,但是当应用程序完成的下载错误时系统执行自动安装时,错误为“ Error receiving broadcast Intent
”,并且“ Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Download/bakul.apk exposed beyond app through Intent.getData()
”
public void Update() {
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "bakul.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
//Delete update file if exists
File file = new File(destination);
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();
//get url of app on server
final String url = "http://192.168.87.87.com/bakul.apk";
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//set destination
request.setDestinationUri(uri);
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(uri,"application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
};
//register receiver for when .apk download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}