android6.0以上的版本,写入权限不仅需要在 manifest文件里书写,还需要进行动态申请。
if (Build.VERSION.SDK_INT >= 23) { int REQUEST_CODE_CONTACT = 101; String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};//验证是否许可权限 for (String str : permissions) { if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) { //申请权限 this.requestPermissions(permissions, REQUEST_CODE_CONTACT); break; } } }
android6.0以上的apk更新安装,采用fileprovider的方法。
1.现在manifest中添加provider
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.qijin.bluetoothapplication.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider>
2.书写xml资源文件。
<paths> <external-path path="Android/data/包名/" name="files_root" /> <external-path path="." name="external_storage_root" /> </paths>
3.更改安装。
private void installApk() { File apkfile = new File(saveFileName); if (!apkfile.exists()) { return; } Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(mContext, "com.qijin.bluetoothapplication.fileprovider", apkfile); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), // "application/vnd.android.package-archive"); mContext.startActivity(intent); callback.OnBackResult("finish"); }