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

来源:http://www.cnblogs.com/shouce/archive/2016/03/16/5282026.html
-Advertisement-
Play Games

Android應用檢查版本更新後,在通知欄下載,更新下載進度,下載完成自動安裝,效果圖如下: AndroidManifest文件中的versionCode用來標識版本,在伺服器放一個新版本的apk,versioncode大於當前版本,下麵代碼用來獲取versioncode的值 用當前versionc


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>
複製代碼 複製代碼
   /**
     * 連接網路,下載一個文件,並傳回進度
     * 
     * @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)) { // 保證同一個數據只發了一次 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); }
複製代碼

安裝完成搞定


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • html5 視頻播放
  • 說明:用線性漸變創建圖像 語法: <linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner> ] ,]? <color-stop>[, <color-stop>]+) <side-or-corner> = [left |
  • iOS 4.0+ 使用英文字體 Helvetica Neue,之前的iOS版本降級使用 Helvetica 中文字體設置為華文黑體STHeiTi 預設數字字體是Helvetica Neue 需補充說明,華文黑體並不存在iOS的字體庫中(http://support.apple.com/kb/HT58
  • 也可以基於這一思路測試某個值是不是原生函數或正則表達式: 方法2、在聲明函數時指定適當的函數,這樣第一次調用函數不損失性能,在代碼首次載入時會損失性能 按下按鈕實際顯示的是undefined,並不會顯示Event handled。問題在於沒有保存handler.handleClick的環境,所以th
  • CSS: HTML: JS:
  • 點擊按鈕,自動生成100個li,紅、黃、藍、綠四種顏色的順序顯示出現在頁面中 CSS: HTML: JS:
  • 一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h RootViewController.m
  • 當內容及分類較多時,往往採用頂部標簽式導航欄,例如網易新聞客戶端的頂部分類導航,最近剛好有這樣的應用場景,參考網路上一些demo,實現了這種導航效果,記錄一些要點。 效果圖(由於視頻轉GIF掉幀,滑動和下拉動畫顯得比較生硬,剛發現quickTime可以直接錄製手機視頻,推薦一下,很方便) 其實就是在
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...