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

首頁 > 系統 > Android > 正文

Android如何實現APP自動更新

2019-12-12 05:29:29
字體:
來源:轉載
供稿:網友

先來看看要實現的效果圖:

對于安卓用戶來說,手機應用市場說滿天飛可是一點都不夸張,比如小米,魅族,百度,360,機鋒,應用寶等等,當我們想上線一款新版本APP時,先不說渠道打包的麻煩,單純指上傳APP到各大應用市場的工作量就已經很大了,好不容易我們把APP都上傳完了,突然發現一個會導致應用閃退的小Bug,這時那個崩潰啊,明明不是很大的改動,難道我們還要再去重新去把各大應用市場的版本再上傳更新一次?相信我,運營人員肯定會弄死你的!!

有問題,自然就會有解決問題的方案,因此我們就會想到如果在APP里內嵌自動更新的功能,那么我們將可以省去很多麻煩,當然關于這方面功能的第三方SDK有很多。

好了,言歸正傳,今天我們自己來實現下關于APP自動更新。

流程其實并不復雜:當用戶打開APP的時候,我們讓APP去發送一個檢查版本的網絡請求,或者利用服務端向APP推送一個透傳消息來檢查APP的版本,如果當前APP版本比服務器上的舊,那么我們就提醒用戶進行下載更新APP,當然在特定的情況下,我們也可以強制的讓用戶去升級,當然這是很不友好的,盡可能的減少這樣的做法。

好了,來梳理下流程,首先既然是一個APP的更新,那么我們就需要去下載新的APP,然后我們需要一個通知來告訴用戶當前的下載進度,再來當APP安裝包下載完成后,我們需要去系統的安裝程序來對APP進行安裝更新。

知識點:

下載:異步HTTP請求文件下載,并監聽當前下載進度(這里我采用了okhttp)

通知:Notification(具體用法請自行翻閱API文檔)

安裝:Intent (具體用法請自行翻閱API文檔)

來看下具體實現代碼:

我們需要一個后臺服務來支撐App的下載

import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.IBinder;import android.support.annotation.Nullable;import android.support.v7.app.NotificationCompat;import com.fangku.commonlibrary.utils.StorageUtil;import com.zhy.http.okhttp.OkHttpUtils;import com.zhy.http.okhttp.callback.FileCallBack;import java.io.File;import okhttp3.Call;/** * 自動下載更新apk服務 * Create by: chenwei.li * Date: 2016-08-14 * time: 09:50 * Email: lichenwei.me@foxmail.com */public class DownloadService extends Service { private String mDownloadUrl;//APK的下載路徑 private NotificationManager mNotificationManager; private Notification mNotification; @Override public void onCreate() { super.onCreate(); mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent == null) {  notifyMsg("溫馨提醒", "文件下載失敗", 0);  stopSelf(); } mDownloadUrl = intent.getStringExtra("apkUrl");//獲取下載APK的鏈接 downloadFile(mDownloadUrl);//下載APK return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void notifyMsg(String title, String content, int progress) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this);//為了向下兼容,這里采用了v7包下的NotificationCompat來構造 builder.setSmallIcon(R.mipmap.icon_login_logo).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_login_logo)).setContentTitle(title); if (progress > 0 && progress < 100) {  //下載進行中  builder.setProgress(100, progress, false); } else {  builder.setProgress(0, 0, false); } builder.setAutoCancel(true); builder.setWhen(System.currentTimeMillis()); builder.setContentText(content); if (progress >= 100) {  //下載完成  builder.setContentIntent(getInstallIntent()); } mNotification = builder.build(); mNotificationManager.notify(0, mNotification); } /** * 安裝apk文件 * * @return */ private PendingIntent getInstallIntent() { File file = new File(StorageUtil.DOWNLOAD_DIR + "APP文件名"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } /** * 下載apk文件 * * @param url */ private void downloadFile(String url) { OkHttpUtils.get().url(url).build().execute(new FileCallBack(StorageUtil.DOWNLOAD_DIR, "APP文件名") {  @Override  public void onError(Call call, Exception e, int id) {  notifyMsg("溫馨提醒", "文件下載失敗", 0);  stopSelf();  }  @Override  public void onResponse(File response, int id) {  //當文件下載完成后回調  notifyMsg("溫馨提醒", "文件下載已完成", 100);  stopSelf();  }  @Override  public void inProgress(float progress, long total, int id) {  //progress*100為當前文件下載進度,total為文件大小  if ((int) (progress * 100) % 10 == 0) {   //避免頻繁刷新View,這里設置每下載10%提醒更新一次進度   notifyMsg("溫馨提醒", "文件正在下載..", (int) (progress * 100));  }  } }); }}

然后我們只需要在我們想要的更新APP的時候去調起這個服務即可,比如在系統設置里的"版本檢查"等

Intent intent = new Intent(mContext, DownloadService.class);intent.putExtra("apkUrl", "APK下載地址");startService(intent);

總結

這里我只是粗略演示本地自動更新APP的功能,在實際應用中,我們應該配合服務端來做,比如在用戶啟動APP的時候去比對版本號,如果版本號低于服務器的版本號,那么此時服務端應該給客戶端一個透傳推送,這里的推送內容應該為新版本APP的下載地址,此時就可以根據該地址來下載新版APP了,當遇到重大更新,不再對老版本進行兼容的時候,可以強制用戶升級,這里的方案有很多,比如調用系統級對話框,讓用戶沒辦法取消等操作,這里就不做更多描述。以上就是這篇文章的全部內容,希望對有需要的人能有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延寿县| 新巴尔虎右旗| 麟游县| 六盘水市| 栾川县| 远安县| 东光县| 汉川市| 木里| 繁昌县| 且末县| 宁国市| 北流市| 通海县| 台中市| 武清区| 宁化县| 瑞昌市| 如东县| 井陉县| 彰化县| 资源县| 镇康县| 土默特左旗| 射阳县| 始兴县| 饶阳县| 东阿县| 灌南县| 肃宁县| 滦平县| 屏东县| 博野县| 长子县| 嵊泗县| 比如县| 永嘉县| 安新县| 宁波市| 鄱阳县| 张家港市|