这是用于打开和安装已下载应用程序的代码; 这种方法问到android的api是它的24向上吗?方法“ apk uri” HTML解释路线。 运行时会崩溃。
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int i = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(i)) {
String uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File file = new File(Environment.getExternalStorageDirectory(), "/Download/AromaPark.apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, "com.sysvoid.aromapark.provider", file);
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setData(apkUri);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
install.setDataAndType(Uri.parse(uri),
"application/vnd.android.package-archive");
activity.startActivity(install);
}
}
}
}
如您所见,这是代码的恢复。 P.s.以获得更多的领悟力。
File file = new File(Environment.getExternalStorageDirectory(), "/Download/AromaPark.apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, "com.sysvoid.aromapark.provider", file);
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setData(apkUri);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
}
This is the XML path:
<?xml version="1.0" encoding="utf-8"?>
<Paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="Download"
path="Download/" />
</Paths>
内部android清单代码
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.sysvoid.aromapark.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
错误在这里运行
Uri apkUri = FileProvider.getUriForFile(activity, "com.sysvoid.aromapark.provider", file);
错误消息
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.sysvoid.aromapark (has extras) } in com.sysvoid.service.Receiver@7f8d274
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_55615(LoadedApk.java:1418)
at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)
at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/AromaPark.apk
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.sysvoid.service.Receiver.onReceive(Receiver.java:53)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_55615(LoadedApk.java:1400)