Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

手機安全衛士實戰(全註解)之七:應用程式的更新安裝

編輯:關於 Android 程式設計

寫在前面:學 Android 時,很多人跟我一樣讀了大量基礎知識,知識點都能說上幾句,但這些零散的點始終不成體系。所以我以經典的「手機安全衛士」專案為例邊做邊練,也歡迎大家一起交流、共同進步。

七、應用程式的更新安裝

當新版本下載成功後(即在 onSuccess 方法中),我們要能跳出系統安裝介面。做法是先寫一個安裝方法:

private void apkInstall(File t) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    // 也可寫成 intent.setAction(android.intent.action.VIEW);
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive");
    startActivity(intent);
}

這裡有一個關鍵要注意:不能用 setData 與 setType 分開設定,必須一次用 setDataAndType 同時指定資料 URI 與 MIME 類型,否則系統無法正確辨識這是一個 APK 安裝檔。

下載完成後呼叫這個方法,就會跳出系統安裝程式,使用者確認後即可完成更新。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved