在我的应用程序中,我有一个自定义的自动下载并安装 APK,它的工作原理是这样的
// auto register for the complete download
activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// Download the file through DownloadManager
String destination = Environment.getExternalStorageDirectory() + "/";
String fileName = "myfile.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setDescription("description");
request.setTitle("title");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
// BEFORE working doing this
//install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//install.setDataAndType(uri,
// manager.getMimeTypeForDownloadedFile(downloadId));
// Using file provider it doesnt work
Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
"com.myapp", file);
install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
activity.unregisterReceiver(this);
}
};
安卓清单:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Provider_path (对不起,由于某种原因,所以削减了路径标签)
外部路径名称=“我的文件夹”路径=“。”/>
当文件完成下载 onComplete 被调用但活动没有开始:
没有找到处理 Intent 的活动 { act=android.intent.action.VIEW dat=content://com.myapp/myfolder/myfile.apk typ=application/vnd.android.package-archive flg=0x4000000 }
使用普通文件时://它确实有效
使用文件提供程序时我缺少什么吗?活动是否因为找不到文件而无法启动?我需要额外的许可吗?(目前我在外部存储上有 INTERNET、READ 和 WRITE)