解決Android更新安裝包時不能自動安裝的問題
阿新 • • 發佈:2018-12-21
一,安裝程式碼
private void installUseAS(String filePath) { File file = new File(filePath); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); 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 contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", file); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(uri, "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } mContext.startActivity(intent); }
二,適配7.0程式碼FileProvider
1,Minifest中配置
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
2,新建file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="images/" /> <files-path name="name" path="path" /> //相當 Context.getFilesDir() + path, name是分享url的一部分 <cache-path name="name" path="path" /> //getCacheDir() <external-path name="name" path="path" /> //Environment.getExternalStorageDirectory() <external-path name="download" path="Download" /> <external-files-path name="name" path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null) <external-cache-path name="name" path="path" /> //Context.getExternalCacheDir() </paths>