關于7.0行為變更厂镇,F(xiàn)ileProvider應用共享文件的問題可以看鴻洋大佬的這篇博客
https://blog.csdn.net/lmj623565791/article/details/72859156
但是我遇到的這個問題是明明有授權臨時權限据过,F(xiàn)ileProvider的XML也有寫踱讨,但是在下載apk后解析安裝包出錯,后面回顧一圈代碼發(fā)現(xiàn)页徐,有問題解取。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.fromFile(response), "application/vnd.android.package-archive");
} else {//Android7.0之后獲取uri要用contentProvider
intent.setDataAndType(Uri.parse("file://" + response.toString()), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//開始安裝
startActivity(intent);
//關閉舊版本的應用程序的進程
android.os.Process.killProcess(android.os.Process.myPid());
intent.setFlags()會覆蓋掉intent.addFlags(),而intent.addFlags只會在之前的后面添加挺尾,所以每次權限是給了鹅搪,但是被去除了。調整下代碼的位置遭铺,問題解決
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.fromFile(response), "application/vnd.android.package-archive");
} else {//Android7.0之后獲取uri要用contentProvider
intent.setDataAndType(Uri.parse("file://" + response.toString()), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
//開始安裝
startActivity(intent);
//關閉舊版本的應用程序的進程
android.os.Process.killProcess(android.os.Process.myPid());