首先,第一個問題:
在Android 7.0 之後, 打開文件時報錯:
其次,第二個問題, 在android 9.0 上,打開intent時會閃一下, 但是打不開, 僅需要添加權限即可
<!--適應android 9.0 調用安裝app-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
最後, android在9.0遇到的解析包錯誤,請看代碼:
未修改之前代碼:
public static void installUPTSMServiceApk(final Activity activity) {
File downNewAppFile = new File(Environment.getExternalStorageDirectory().getPath() + "/GDDownload", "UPTSMSeraviceApk.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".fileProvider", downNewAppFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(downNewAppFile), "application/vnd.android.package-archive");
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
修改之後代碼:
public static void installUPTSMServiceApk(final Activity activity) {
// 啓用安裝新線程
File downNewAppFile = new File(Environment.getExternalStorageDirectory().getPath() + "/GDDownload", "UPTSMSeraviceApk.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 24) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".fileProvider", downNewAppFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(downNewAppFile), "application/vnd.android.package-archive");
}
activity.startActivity(intent);
}
如果大家仔細看代碼就會看出些許端倪, 如果沒時間看也沒關係,其實最主要的就是把
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
放在了,
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
的前面, 具體原因不再詳述,有興趣的同學可以自行查閱。