欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年03月16日 09:37:00   投稿:mrr  
Android應(yīng)用檢查版本更新后,在通知欄下載,更新下載進度,下載完成自動安裝。接下來通過本文給大家介紹Android程序版本更新之通知欄更新下載安裝的相關(guān)知識,需要的朋友參考下吧

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

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

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

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

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

•下載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" >
<ImageView
android:id="@+id/notify_icon_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_launcher" />
<TextView
android: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" />
<ProgressBar
android: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ò),下載一個文件,并傳回進度
* 
* @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() {
@Override
public void run() {
super.run();
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream in = conn.getInputStream();
// 2865412
long length = conn.getContentLength();
// 每次讀取1k
byte[] 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)前進度
int schedule = (int) ((file.length() * 100) / length);
// 通知更新進度(10,7,4整除才通知,沒必要每次都更新進度)
if (temp != schedule
&& (schedule % 10 == 0 || schedule % 4 == 0 || schedule % 7 == 0)) {
// 保證同一個數(shù)據(jù)只發(fā)了一次
temp = schedule;
handler.sendEmptyMessage(schedule);
}
}
out.flush();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}

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

•更新通知欄進度條

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

安裝完成搞定

相關(guān)文章

  • Android實現(xiàn)圖片輪播效果的兩種方法

    Android實現(xiàn)圖片輪播效果的兩種方法

    android圖片輪播效果非常漂亮,在程序開發(fā)中也經(jīng)常用到,本文給大家分享android實現(xiàn)圖片輪播效果的幾種方法,對android實現(xiàn)圖片輪播相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android三種菜單實例分析

    Android三種菜單實例分析

    這篇文章主要介紹了Android三種菜單,較為詳細(xì)的分析了Android菜單分類及相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • Android 調(diào)用發(fā)送短信的方法

    Android 調(diào)用發(fā)送短信的方法

    這篇文章主要介紹了Android 調(diào)用發(fā)送短信的方法的相關(guān)資料,主要實現(xiàn)Android 調(diào)用短信的使用,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 淺談onTouch先執(zhí)行,還是onClick執(zhí)行(詳解)

    淺談onTouch先執(zhí)行,還是onClick執(zhí)行(詳解)

    onTouch先執(zhí)行,還是onClick執(zhí)行?下面小編就為大家?guī)硪黄獪\談onTouch先執(zhí)行,還是onClick執(zhí)行(詳解)。希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-03-03
  • Android自定義View實現(xiàn)游戲搖桿鍵盤的方法示例

    Android自定義View實現(xiàn)游戲搖桿鍵盤的方法示例

    Android進階過程中有一個繞不開的話題——自定義View。最近在做項目中又遇到了,所以下面這篇文章主要給大家介紹了利用Android自定義View實現(xiàn)游戲搖桿鍵盤的相關(guān)資料,操作方式類似王者榮耀的搖桿操作,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。
    2017-07-07
  • 淺談Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File Explorer

    淺談Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File

    這篇文章主要介紹了淺談Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File Explorer的相關(guān)資料,需要的朋友可以參考下
    2017-11-11
  • 安卓版微信小程序跳一跳輔助

    安卓版微信小程序跳一跳輔助

    這篇文章主要為大家詳細(xì)介紹了安卓版微信小程序跳一跳輔助,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android中屏幕密度和圖片大小的關(guān)系詳解

    Android中屏幕密度和圖片大小的關(guān)系詳解

    這篇文章主要介紹了Android中屏幕密度和圖片大小的關(guān)系詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • android ListView的右邊滾動滑塊啟用方法 分享

    android ListView的右邊滾動滑塊啟用方法 分享

    android ListView的右邊滾動滑塊啟用方法 分享,需要的朋友可以參考一下
    2013-05-05
  • Android編程開發(fā)音樂播放器實例

    Android編程開發(fā)音樂播放器實例

    這篇文章主要介紹了Android編程開發(fā)音樂播放器,結(jié)合實例形式分析了Android音樂播放器開發(fā)所涉及的SeekBar,ListView,廣播接收者(以代碼的形式注冊Receiver),系統(tǒng)服務(wù),MediaPlayer等技巧,需要的朋友可以參考下
    2016-01-01

最新評論