安卓7.0線上更新解析包時出現問題
關於7.0行為變更,FileProvider應用共享檔案的問題可以看鴻洋大佬的這篇部落格
ofollow,noindex">https://blog.csdn.net/lmj623565791/article/details/72859156
但是我遇到的這個問題是明明有授權臨時許可權,FileProvider的XML也有寫,但是在下載apk後解析安裝包出錯,後面回顧一圈程式碼發現,有問題。
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());