編輯: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();
}
在通知欄設定進度條可見性時,可能會莫名崩潰:
// 這句話沒有語法錯誤,卻會導致程式出錯 // 解法:在 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);
如此一來,通知欄就能完整呈現從下載到安裝的全流程。若這篇文章對你有幫助,歡迎給個推薦,感謝支持!
Android 加載 Spinner 實作指南:ProgressBar 樣式、生命週期與解除時機
在 Android 中,任務的執行進展可以透過載入進度條來呈現。進度條有兩種形態:加載欄與加載微調(spinner)。前者用於能算出百分比的场合,後者則是給那些「還要多久
Android Activity 生命週期完整解析
Activity 啟動時通常依序經過 onCreate、onStart、onResume。按 BACK 鍵會走 onPause、onStop、onDestroy;按 HO
Android Spinner 載入提示元件:屬性、方法與使用時機
App 在背後做事的時候,使用者需要知道「它還活著」。Android 用 ProgressBar 負責這件事,而它有兩種典型外觀:可以標示百分比的長條進度條,以及不斷旋轉
Android Spinner 載入動畫:不確定進度的處理方式
當 App 執行某項工作時,使用者需要一個「還在處理中」的視覺回饋。Android 的 ProgressBar 提供兩種外觀:一种是能顯示百分比的橫向進度列,另一種是持續