最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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" >
<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)絡,下載一個文件,并傳回進度
* 
* @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);
// 當前進度
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);
}

安裝完成搞定

相關文章

  • Flutter 狀態(tài)管理的實現(xiàn)

    Flutter 狀態(tài)管理的實現(xiàn)

    這篇文章主要介紹了Flutter 狀態(tài)管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Android實現(xiàn)翻頁特效

    Android實現(xiàn)翻頁特效

    這篇文章主要為大家詳細介紹了Android實現(xiàn)翻頁特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android網(wǎng)格布局GridView學習使用

    Android網(wǎng)格布局GridView學習使用

    這篇文章主要為大家詳細介紹了Android網(wǎng)格布局GirdView的學習使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 詳解Android 藍牙通信方式總結(jié)

    詳解Android 藍牙通信方式總結(jié)

    這篇文章主要介紹了詳解Android 藍牙通信方式總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2013-11-11
  • Android編程實現(xiàn)拍照功能的2種方法分析

    Android編程實現(xiàn)拍照功能的2種方法分析

    這篇文章主要介紹了Android編程實現(xiàn)拍照功能的2種方法,結(jié)合具體實例形式對比分析了Android通過調(diào)用系統(tǒng)攝像頭及程序調(diào)用照相機功能兩種實現(xiàn)技巧與相關注意事項,需要的朋友可以參考下
    2017-07-07
  • android 仿微信demo——注冊功能實現(xiàn)(移動端)

    android 仿微信demo——注冊功能實現(xiàn)(移動端)

    本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助
    2021-06-06
  • Android Studio導入第三方類庫的方法

    Android Studio導入第三方類庫的方法

    這篇文章主要介紹了Android Studio導入第三方類庫的方法,導入*.jar包、導入第三方java類庫含源碼包以及aar的引入,需要的朋友可以參考下
    2016-07-07
  • Android WebView 緩存詳解

    Android WebView 緩存詳解

    這篇文章主要介紹了 Android WebView 緩存詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • Android ViewPager循環(huán)播放廣告實例詳解

    Android ViewPager循環(huán)播放廣告實例詳解

    這篇文章主要介紹了Android ViewPager循環(huán)播放廣告條實例詳解的相關資料,需要的朋友可以參考下
    2017-03-03
  • android中設置TextView/Button 走馬燈(Marquee)效果示例

    android中設置TextView/Button 走馬燈(Marquee)效果示例

    定義走馬燈(Marquee),主要在Project/res/layout/main.xml即可,下面與大家分享下具體的實現(xiàn),感興趣的朋友可以參考下哈
    2013-06-06

最新評論

翼城县| 井陉县| 修文县| 苍梧县| 庆城县| 崇仁县| 泰和县| 正镶白旗| 博湖县| 桑植县| 晴隆县| 余姚市| 赫章县| 邢台市| 鸡泽县| 甘肃省| 邛崃市| 博乐市| 西青区| 大姚县| 商城县| 高淳县| 云浮市| 台江县| 鄱阳县| 临沂市| 双城市| 报价| 长汀县| 东阿县| 阳泉市| 杭锦后旗| 南和县| 久治县| 资讯 | 遵化市| 龙州县| 定日县| 凉城县| 常熟市| 乌兰察布市|