Android學習系列(3)–App自動更新之自訂進度視圖和內部儲存

來源:互聯網
上載者:User

友好的視覺感知和穩定的不出錯表現,來自於我們追求美感和考慮的全面性,部落格園從技術的角度,一直我都很欣賞。
這篇文章是android開發人員的必備知識,是我特別為大家整理和總結的,不求完美,但是有用。 

這一篇是對上一篇《Android學習系列(2)--App自動更新之通知欄下載》的補充,因此只是以點為要,點到為止。
1.內部儲存
    出於考慮到使用者可能禁掉了SDCard或者電腦暫時插在電腦上且為磁碟串連狀態等等,對於這麼個情況下,我們應該也要保證我們的程式也是能正常的運行。所以我們要考慮內部儲存。
    我暫時把內部儲存定在/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");

 2.內部儲存的許可權
  一起都啟動並執行正常,但是當我們幫下下來的apk檔案執行運行操作的時候,確提示如下,
 
    "解析包錯誤"??其實你下載的檔案並不一定就是壞的或者錯誤的,也可能是android系統的許可權在作怪。在你執行之前,加上如下核心代碼:

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

3.通知欄顯示進度條組件的一個bug。
    在通知欄設定進度條的可見度,會無緣無故的崩潰。

//下面一句是沒有語法錯誤的,但是會導致程式出錯//為瞭解決這個問題,後面我們會再progressView外麵包裹一層LinearLayout來控制可見度updateNotification.contentView.setViewVisibility(progressViewID, View.GONE);

4.自訂進度條顯示視圖。
    布局檔案updata_nitification.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://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);

  如下:
   如果你喜歡的話,請推薦一下,謝謝大家的支援! 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.