Android項目實戰(四十):Andoird 7.0+ 安裝APK適配

來源:互聯網
上載者:User

標籤:系統   cat   成功   package   get   man   acm   fromfile   VID   

原文:Android項目實戰(四十):Andoird 7.0+ 安裝APK適配

  

   首先看一下安裝apk檔案的代碼

    /**     * 通過隱式意圖調用系統安裝程式安裝APK     */    public static void install(Context context) {        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        intent.setDataAndType(Uri.fromFile(                new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "xxxx.apk")),                "application/vnd.android.package-archive");        context.startActivity(intent);    }

  

    測試發現該段代碼在7.0一下的機型上可以成功開啟指定路徑下的指定apk檔案 , 但是在7.0+的機型上調用該代碼會報錯:

   android.os.FileUriExposedException: file:///storage/emulated/0/Download/xxxx.apk exposed beyond app through Intent.getData()

 

 

     原因在於:Android 7.0 版本開始  禁止向你的應用外公開 file:// URI。 如果一項包含檔案 file:// URI類型 的 Intent 離開你的應用,應用失敗,並出現 FileUriExposedException 異常。

   

  解決方案:

  一、在AndroidManifest.xml 檔案中添加 四大組件之一的 <provider>

    

 <!-- 適配7.0 apk安裝 -->        <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="com.xxx.xxxx.fileprovider"             android:grantUriPermissions="true"            android:exported="false">            <!--中繼資料-->            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths" />        </provider>

 

   注意這裡的  android :authorities 屬性的值 中的 com.xxx.xxxx 是你的包名,不可隨意填寫

 

  二、res 目錄下 建一個xml 檔案,並建立xml檔案file_paths.xml 

    注意檔案名稱要和第一步中的 resource 屬性的值一致 

    內容為:

<?xml version="1.0" encoding="utf-8"?><paths>    <external-path path="." name="download"/></paths>

 

 

  

  三、根據機型的Android系統層級執行不同的安裝apk的代碼

      注意,根據系統版本執行不同代碼,7.0以下調用7.0+的代碼會報錯,7.0+的調用7.0以下的會報錯。

      if (file!=null){   // file 即 apk檔案                Intent intent = new Intent(Intent.ACTION_VIEW);                // 由於沒有在Activity環境下啟動Activity,設定下面的標籤                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                if(Build.VERSION.SDK_INT>=24) { //判讀版本是否在7.0以上                    //參數1 上下文, 參數2 Provider主機地址 和設定檔中保持一致   參數3  共用的檔案                    Uri apkUri =                            FileProvider.getUriForFile(context, "com.xxx.xxxxx.fileprovider", file);                    //添加這一句表示對目標應用臨時授權該Uri所代表的檔案                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");                }else{                    intent.setDataAndType(Uri.fromFile(file),                            "application/vnd.android.package-archive");                }                context.startActivity(intent);            }

 

Android項目實戰(四十):Andoird 7.0+ 安裝APK適配

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.