Android程式版本更新之通知欄更新下載安裝_Android

來源:互聯網
上載者:User

Android應用檢查版本更新後,在通知欄下載,更新下載進度,下載完成自動安裝,效果圖如下:

•檢查目前的版本號

AndroidManifest檔案中的versionCode用來標識版本,在伺服器放一個新版本的apk,versioncode大於目前的版本,下面代碼用來擷取versioncode的值

PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);int localVersion = packageInfo.versionCode; 

用當前versioncode和服務端比較,如果小於,就進行版本更新

•下載apk檔案

/*** 下載apk* * @param apkUri*/private void downLoadNewApk(String apkUri, String version) {manager = (NotificationManager) context.getSystemService((context.NOTIFICATION_SERVICE));notify = new Notification();notify.icon = R.drawable.ic_launcher;// 通知欄顯示所用到的布局檔案notify.contentView = new RemoteViews(context.getPackageName(),R.layout.view_notify_item);manager.notify(100, notify);//建立下載的apk檔案File fileInstall = FileOperate.mkdirSdcardFile("downLoad", APK_NAME+ version + ".apk");downLoadSchedule(apkUri, completeHandler, context,fileInstall);}

FileOperate是自己寫的檔案工具類

通知欄顯示的布局,view_notify_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:background="#00000000"android:padding="5dp" ><ImageViewandroid:id="@+id/notify_icon_iv"android:layout_width="25dp"android:layout_height="25dp"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/notify_updata_values_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="6dp"android:layout_marginLeft="15dp"android:layout_marginTop="5dp"android:layout_toRightOf="@id/notify_icon_iv"android:gravity="center_vertical"android:text="0%"android:textColor="@color/white"android:textSize="12sp" /><ProgressBarandroid:id="@+id/notify_updata_progress"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/notify_icon_iv"android:layout_marginTop="4dp"android:max="100" /></RelativeLayout> /*** 串連網路,下載一個檔案,並傳回進度* * @param uri* @param handler* @param context* @param file*/public static void downLoadSchedule(final String uri,final Handler handler, Context context, final File file) {if (!file.exists()) {handler.sendEmptyMessage(-1);return;}// 每次讀取檔案的長度final int perLength = 4096;new Thread() {@Overridepublic void run() {super.run();try {URL url = new URL(uri);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);conn.connect();InputStream in = conn.getInputStream();// 2865412long length = conn.getContentLength();// 每次讀取1kbyte[] buffer = new byte[perLength];int len = -1;FileOutputStream out = new FileOutputStream(file);int temp = 0;while ((len = in.read(buffer)) != -1) {// 寫入檔案out.write(buffer, 0, len);// 當前進度int schedule = (int) ((file.length() * 100) / length);// 通知更新進度(10,7,4整除才通知,沒必要每次都更新進度)if (temp != schedule&& (schedule % 10 == 0 || schedule % 4 == 0 || schedule % 7 == 0)) {// 保證同一個資料只發了一次temp = schedule;handler.sendEmptyMessage(schedule);}}out.flush();out.close();in.close();} catch (IOException e) {e.printStackTrace();}}}.start();}

handler根據下載進度進行更新

•更新通知欄進度條

/*** 更新通知欄*/ private Handler completeHandler = new Handler() {public void handleMessage(android.os.Message msg) {// 更新通知欄if (msg.what < 100) {notify.contentView.setTextViewText(R.id.notify_updata_values_tv, msg.what + "%");notify.contentView.setProgressBar(R.id.notify_updata_progress,100, msg.what, false);manager.notify(100, notify);} else {notify.contentView.setTextViewText(R.id.notify_updata_values_tv, "下載完成");notify.contentView.setProgressBar(R.id.notify_updata_progress,100, msg.what, false);// 清除通知欄manager.cancel(100);installApk(fileInstall);}};}; 

下載完成後調用系統安裝。

•安裝apk

/*** 安裝apk* * @param file*/private void installApk(File file) {Intent intent = new Intent();intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setAction(android.content.Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");context.startActivity(intent);}

安裝完成搞定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.