7.0自動(dòng)更新的問(wèn)題(完整方案)

問(wèn)題所在

事情是這樣的,前幾天,發(fā)現(xiàn)有幾個(gè)手機(jī)升級(jí)應(yīng)用失敗,查了下,發(fā)現(xiàn)都是7.0的機(jī)器,于是查了下google的7.0API描述,發(fā)現(xiàn)了這個(gè)東西


google_doc

ps:google的文檔中文化真的很高,現(xiàn)在。

完整解決方案

以前我們是怎么處理下載問(wèn)題的呢?

下載

private static void downloadNewVersionApp(Context context, String url) {
  DownloadManager downloadManager =
      (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  Uri uri = Uri.parse(url);
  DownloadManager.Request request = new DownloadManager.Request(uri);
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getUpdateFile());
  request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
      | DownloadManager.Request.NETWORK_WIFI);
  request.setVisibleInDownloadsUi(false);
  long id = downloadManager.enqueue(request);
  AppCache.appDownloadId = id;
}

下載完成回調(diào)

private DownloadManager downloadManager;

@Override
public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    if (id != AppCache.appDownloadId) {
      return;
    }
    IntentUtils.installApk(context);
  }
}

安裝

public static void installApk(Context context) {
  File file= new File(
      Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
      , FishUtils.getUpdateFile());
  if (file == null) return;
  if (!file.exists()) return;
  Intent intent = new Intent();
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.setAction(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  context.startActivity(intent);
}

可以看到以前我們確實(shí)是用URI傳遞地址了,但是現(xiàn)在不行了。所以,要判斷版本

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
      installAPKGreaterThanM(context,file);
      return;
    }

7.0 后更新

因?yàn)間oogleAPI的限制,這里我們需要使用FileProvider

<provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="com.XXXX.fileprovider"
      android:grantUriPermissions="true"
      android:exported="false">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
  </provider>

exported:要求必須為false,為true則會(huì)報(bào)安全異常。
grantUriPermissions:true,表示授予 URI 臨時(shí)訪問(wèn)權(quán)限。
authorities 組件標(biāo)識(shí),按照江湖規(guī)矩,都以包名開頭,避免和其它應(yīng)用發(fā)生沖突。

看下我們指定的@xml/file_paths

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
  <external-path path="" name="download"/>
</paths>
</resources>

這里的path=""指的是根目錄,即可以其他應(yīng)用共享根目錄和子目錄下的所有資源

恩,然后看下怎么使用

private static void installAPKGreaterThanM(Context context,File file) {
  Uri apkUri =
      FileProvider.getUriForFile(context, "com.XXXX.fileprovider", file);
  Intent intent = new Intent(Intent.ACTION_VIEW);
  // 由于沒(méi)有在Activity環(huán)境下啟動(dòng)Activity,設(shè)置下面的標(biāo)簽
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  context.startActivity(intent);
}

大功告成!?。?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容