乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Android 在代碼中安裝 APK 文件

       jnstyle 2019-02-25

       http://www.cnblogs.com/newjeremy/p/7294519.html

      話不說,上代碼

      復(fù)制代碼
          private void install(String filePath) {
              Log.i(TAG, "開始執(zhí)行安裝: " + filePath);
              File apkFile = new File(filePath);
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                  Log.w(TAG, "版本大于 N ,開始使用 fileProvider 進(jìn)行安裝");
                  intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                  Uri contentUri = FileProvider.getUriForFile(
                          mContext
                          , "你的包名.fileprovider"
                          , apkFile);
                  intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
              } else {
                  Log.w(TAG, "正常進(jìn)行安裝");
                  intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
              }
              startActivity(intent);
          }
      復(fù)制代碼

       代碼說明

      關(guān)于在代碼中安裝 APK 文件,在 Android N 以后,為了安卓系統(tǒng)為了安全考慮,不能直接訪問軟件,需要使用 fileprovider 機(jī)制來訪問、打開 APK 文件。

      上面的 if 語句,就是區(qū)分軟件運行平臺,來對 intent 設(shè)置不同的屬性。

      適配 Android 各個版本,使用代碼安裝 APK

      第一步:

      在清單文件(manifests.xml)application 標(biāo)簽中增加 <provider> 標(biāo)簽:

      復(fù)制代碼
          <application>
              <!--其他的配置項-->
              <provider
                  android:name="android.support.v4.content.FileProvider"
                  android:authorities="你的包名.fileprovider"
                  android:exported="false"
                  android:grantUriPermissions="true">
                  <meta-data
                      android:name="android.support.FILE_PROVIDER_PATHS"
                      android:resource="@xml/file_paths" />
              </provider>
              <!--其他的配置項-->
          </application>    
      復(fù)制代碼

      注意兩點內(nèi)容:

      1. android:authorities="你的包名.fileprovider" 這個屬性要設(shè)置成你自己的包名。

      2. <meta-data> 標(biāo)簽下 android:resource="@xml/file_paths" 是要配置的 xml 文件,他的內(nèi)容如下:

      第二步:

      在 res/xml 下增加文件: file_paths.xml 該文件內(nèi)容如下:

      復(fù)制代碼
      <?xml version="1.0" encoding="utf-8"?>
      <paths>
          <external-path
              name="your_name"
              path="your_path" />
      </paths>
      復(fù)制代碼

      上面的兩個屬性要根據(jù)自己的使用來配置。

      其中 <external-path> 就是手機(jī)的外置存儲目錄。

      第三步:

      在 Java 代碼中使用最上面的代碼,問題解決。

      這里面有個要注意的點:

      清單文件中的 android:authorities="你的包名.fileprovider" 和 JAVA 代碼中:

      Uri contentUri = FileProvider.getUriForFile(
                          mContext
                          , "你的包名.fileprovider"
                          , apkFile);

      綠色背景的字段必須一致,否則會報錯。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多