Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 開發系列(三):App 自動更新——自訂進度視圖與內部儲存

編輯:Android 開發實例

友好的視覺呈現與穩定不出錯的表現,來自於對細節的全面考量。本文是上一篇〈App 自動更新之通知欄下載〉的延伸補充,因此只聚焦重點,點到為止。

一、內部儲存

使用者可能停用 SD 卡,或手機正以磁碟模式連上電腦,這些情況下外部儲存未必可用。為了確保程式隨時都能正常運行,我們必須把內部儲存納入考量。以下將內部儲存定位在 /data/data/xxxxxappxxxx/files 目錄,核心程式碼如下:

// 建立目錄與檔案
if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
 updateDir = new File(Environment.getExternalStorageDirectory(), Global.downloadDir);
} else {
 // files 目錄
 updateDir = getFilesDir();
}
updateFile = new File(updateDir.getPath(), getResources().getString(titleId) + ".apk");

二、內部儲存的權限問題

下載過程看似一切正常,但當我們試圖執行下載好的 apk 時,卻跳出「解析包錯誤」。這時檔案本身未必有問題,很可能是系統權限在作怪。執行前先加上這段程式碼即可:

String cmd = "chmod +x " + updateFile.getPath();
try {
 Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
 e.printStackTrace();
}

三、通知欄進度條可見性的一個 Bug

在通知欄設定進度條可見性時,可能會莫名崩潰:

// 這句話沒有語法錯誤,卻會導致程式出錯
// 解法:在 progressView 外層包一個 LinearLayout 來控制可見性
updateNotification.contentView.setViewVisibility(progressViewID, View.GONE);

四、自訂進度條顯示視圖

佈局檔 updata_nitification.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="vertical" android:layout_weight="2" android:paddingLeft="5dip">
 <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:gravity="left|center_vertical" android:orientation="horizontal" android:layout_weight="1">
  <ImageView android:src="@drawable/icon" android:layout_width="24dip"
   android:layout_height="fill_parent" android:scaleType="fitCenter" />
  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:text="@string/app_name" android:textColor="#000000"
   android:paddingLeft="5dip" android:textSize="16dip" />
 </LinearLayout>
 <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:gravity="left" android:orientation="horizontal" android:layout_weight="1">
  <TextView android:id="@+id/update_notification_progresstext"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:textColor="#8F8F8F" android:textSize="14dip" />
  <LinearLayout android:id="@+id/update_notification_progressblock"
   android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
   <ProgressBar android:id="@+id/update_notification_progressbar"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>

開始下載時:

updateNotification.contentIntent = updatePendingIntent;
updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100, 0, false);
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "0%");

下載進行中,更新進度條:

updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100, (int)(totalSize*100/updateTotalSize), false);
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, (int)(totalSize*100/updateTotalSize) + "%");
updateNotificationManager.notify(0, updateNotification);

下載完成,點擊即可安裝:

// 點擊安裝的 PendingIntent
Uri uri = Uri.fromFile(updateFile);
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0);
updateNotification.defaults = Notification.DEFAULT_SOUND; // 鈴聲提醒
updateNotification.contentIntent = updatePendingIntent; // 安裝介面
updateNotification.contentView.setViewVisibility(com.cnblogs.tianxia.subway.R.id.update_notification_progressblock, View.GONE);
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "下載完成,點擊安裝!");
updateNotificationManager.notify(0, updateNotification);

如此一來,通知欄就能完整呈現從下載到安裝的全流程。若這篇文章對你有幫助,歡迎給個推薦,感謝支持!

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