Android程序版本更新--通知栏更新下载安装

十度 Android 2016年03月16日 收藏

Android应用检查版本更新后,在通知栏下载,更新下载进度,下载完成自动安装,效果图如下:

  • 检查当前版本号

AndroidManifest文件中的versionCode用来标识版本,在服务器放一个新版本的apk,versioncode大于当前版本,下面代码用来获取versioncode的值

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

用当前versioncode和服务端比较,如果小于,就进行版本更新

  • 下载apk文件
  1. /**
  2. * 下载apk
  3. *
  4. * @param apkUri
  5. */
    private void downLoadNewApk(String apkUri, String version) {
  6. manager = (NotificationManager) context
  7. .getSystemService((context.NOTIFICATION_SERVICE));
  8. notify = new Notification();
  9. notify.icon = R.drawable.ic_launcher;
  10. // 通知栏显示所用到的布局文件
  11. notify.contentView = new RemoteViews(context.getPackageName(),
  12. R.layout.view_notify_item);
  13. manager.notify(100, notify);
  14. //建立下载的apk文件
  15. File fileInstall = FileOperate.mkdirSdcardFile("downLoad", APK_NAME
  16. + version + ".apk");
  17. downLoadSchedule(apkUri, completeHandler, context,
  18. fileInstall);
  19. }
  1. FileOperate是自己写的文件工具类

通知栏显示的布局,view_notify_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:layout_marginLeft="10dp"
  6. android:background="#00000000"
  7. android:padding="5dp" >
  8.  
  9. <ImageView
  10. android:id="@+id/notify_icon_iv"
  11. android:layout_width="25dp"
  12. android:layout_height="25dp"
  13. android:src="@drawable/ic_launcher" />
  14.  
  15. <TextView
  16. android:id="@+id/notify_updata_values_tv"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_centerHorizontal="true"
  20. android:layout_marginBottom="6dp"
  21. android:layout_marginLeft="15dp"
  22. android:layout_marginTop="5dp"
  23. android:layout_toRightOf="@id/notify_icon_iv"
  24. android:gravity="center_vertical"
  25. android:text="0%"
  26. android:textColor="@color/white"
  27. android:textSize="12sp" />
  28.  
  29. <ProgressBar
  30. android:id="@+id/notify_updata_progress"
  31. style="@android:style/Widget.ProgressBar.Horizontal"
  32. android:layout_width="fill_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_below="@id/notify_icon_iv"
  35. android:layout_marginTop="4dp"
  36. android:max="100" />
  37. </RelativeLayout>
  1. /**
  2. * 连接网络,下载一个文件,并传回进度
  3. *
  4. * @param uri
  5. * @param handler
  6. * @param context
  7. * @param file
  8. */
    public static void downLoadSchedule(final String uri,
  9. final Handler handler, Context context, final File file) {
  10. if (!file.exists()) {
  11. handler.sendEmptyMessage(-1);
  12. return;
  13. }
  14. // 每次读取文件的长度
  15. final int perLength = 4096;
  16. new Thread() {
  17. @Override
  18. public void run() {
  19. super.run();
  20. try {
  21. URL url = new URL(uri);
  22. HttpURLConnection conn = (HttpURLConnection) url
  23. .openConnection();
  24. conn.setDoInput(true);
  25. conn.connect();
  26. InputStream in = conn.getInputStream();
  27. // 2865412
  28. long length = conn.getContentLength();
  29. // 每次读取1k
  30. byte[] buffer = new byte[perLength];
  31. int len = -1;
  32. FileOutputStream out = new FileOutputStream(file);
  33. int temp = 0;
  34. while ((len = in.read(buffer)) != -1) {
  35. // 写入文件
  36. out.write(buffer, 0, len);
  37. // 当前进度
  38. int schedule = (int) ((file.length() * 100) / length);
  39. // 通知更新进度(10,7,4整除才通知,没必要每次都更新进度)
  40. if (temp != schedule
  41. && (schedule % 10 == 0 || schedule % 4 == 0 || schedule % 7 == 0)) {
  42. // 保证同一个数据只发了一次
  43. temp = schedule;
  44. handler.sendEmptyMessage(schedule);
  45. }
  46. }
  47. out.flush();
  48. out.close();
  49. in.close();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }.start();
  55. }

handler根据下载进度进行更新

  • 更新通知栏进度条
  1. /**
  2. * 更新通知栏
  3. */
    private Handler completeHandler = new Handler() {
  4. public void handleMessage(android.os.Message msg) {
  5. // 更新通知栏
  6. if (msg.what < 100) {
  7. notify.contentView.setTextViewText(
  8. R.id.notify_updata_values_tv, msg.what + "%");
  9. notify.contentView.setProgressBar(R.id.notify_updata_progress,
  10. 100, msg.what, false);
  11. manager.notify(100, notify);
  12. } else {
  13. notify.contentView.setTextViewText(
  14. R.id.notify_updata_values_tv, "下载完成");
  15. notify.contentView.setProgressBar(R.id.notify_updata_progress,
  16. 100, msg.what, false);// 清除通知栏
  17. manager.cancel(100);
  18. installApk(fileInstall);
  19. }
  20. };
  21. };

下载完成后调用系统安装。

  • 安装apk
  1. /**
  2. * 安装apk
  3. *
  4. * @param file
  5. */
    private void installApk(File file) {
  6. Intent intent = new Intent();
  7. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8. intent.setAction(android.content.Intent.ACTION_VIEW);
  9. intent.setDataAndType(Uri.fromFile(file),
  10. "application/vnd.android.package-archive");
  11. context.startActivity(intent);
  12. }

安装完成搞定