国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2020-01-02 07:00:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android應(yīng)用檢查版本更新后,在通知欄下載,更新下載進(jìn)度,下載完成自動(dòng)安裝,效果圖如下:

•檢查當(dāng)前版本號(hào)

AndroidManifest文件中的versionCode用來(lái)標(biāo)識(shí)版本,在服務(wù)器放一個(gè)新版本的apk,versioncode大于當(dāng)前版本,下面代碼用來(lái)獲取versioncode的值

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

用當(dāng)前versioncode和服務(wù)端比較,如果小于,就進(jìn)行版本更新

•下載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> /*** 連接網(wǎng)絡(luò),下載一個(gè)文件,并傳回進(jìn)度* * @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;}// 每次讀取文件的長(zhǎng)度f(wàn)inal 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);// 當(dāng)前進(jìn)度int schedule = (int) ((file.length() * 100) / length);// 通知更新進(jìn)度(10,7,4整除才通知,沒(méi)必要每次都更新進(jìn)度)if (temp != schedule&& (schedule % 10 == 0 || schedule % 4 == 0 || schedule % 7 == 0)) {// 保證同一個(gè)數(shù)據(jù)只發(fā)了一次temp = schedule;handler.sendEmptyMessage(schedule);}}out.flush();out.close();in.close();} catch (IOException e) {e.printStackTrace();}}}.start();}

handler根據(jù)下載進(jìn)度進(jìn)行更新

•更新通知欄進(jìn)度條

/*** 更新通知欄*/ 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);}};}; 

下載完成后調(diào)用系統(tǒng)安裝。

•安裝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);}

安裝完成搞定

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 马山县| 方城县| 墨玉县| 六枝特区| 成安县| 康平县| 桦甸市| 泾阳县| 怀化市| 枞阳县| 金昌市| 屯留县| 合肥市| 南涧| 康平县| 房山区| 温宿县| 勐海县| 重庆市| 青铜峡市| 桃园县| 花莲市| 旌德县| 武功县| 通化市| 汨罗市| 香港 | 开平市| 西藏| 沿河| 逊克县| 昆山市| 汾西县| 东丰县| 长治市| 林甸县| 苏尼特右旗| 益阳市| 洛浦县| 赣榆县| 宜宾市|