android版本過高導致下載軟體後無法自動安裝
阿新 • • 發佈:2019-01-29
安卓6.0以上可能會出現後臺下載軟體後無法自動安裝。
原因是:6.0以上的系統獲取到手機路徑不對,所以系統找不到下載後的軟體
解決方法:1.在配置檔案AndroidManifest.xml中的節點下加入如下程式碼
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="(這裡填包名).fileProvider"
android:exported="false"
android:grantUriPermissions ="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
2.在res目錄下新建xml資料夾,並新建一個provider_paths.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android ="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/(這裡是包名)/" />
<external-path
name="external_storage_root"
path="." />
</paths>
3.最後,在activity中安裝軟體的地方修改為如下程式碼
Intent intent = new Intent(Intent.ACTION _VIEW);
//這裡是判斷android版本,如果版本較高的根據FileProvider獲取路徑
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", new File(apkPath + "/" + apkName));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(apkPath + "/" + apkName)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
荊軻刺秦王!