Android7.0+安裝apk檔案之後不彈出安裝完成的介面解決辦法
在Android7.0+手上,版本升級完成,發現手機安裝完成,不啟動安裝完成頁面,而是直接關閉了,小編也是一頭霧水。琢磨了很久,下面小編把解決辦法show出來。
第一步:在資原始檔下面新建 xml資料夾,新建file_paths.xml檔案,程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="files_root"
path="Android/data/com.ymcd.ivappa/" />
<external-path
name="external_storage_root"
path="." />
</paths>
第二步,在AndroidManifest.xml檔案配置裡面新增
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ymcd.ivappa.fileProvider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
第三步,在程式碼裡面新增下面核心程式碼
/**
* 安裝檔案
*
* @param mContext
* @param file
*/
private void installApk(Context mContext, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// 判斷版本大於等於7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 給目標應用一個臨時授權
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri data = FileProvider.getUriForFile(mContext, "com.example.administrator.newspolice.fileProvider", file);
intent.setDataAndType(data, "application/vnd.android.package-archive");
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
startActivity(intent);
}