Android如何實現(xiàn)APP自動更新
先來看看要實現(xiàn)的效果圖:

對于安卓用戶來說,手機應(yīng)用市場說滿天飛可是一點都不夸張,比如小米,魅族,百度,360,機鋒,應(yīng)用寶等等,當我們想上線一款新版本APP時,先不說渠道打包的麻煩,單純指上傳APP到各大應(yīng)用市場的工作量就已經(jīng)很大了,好不容易我們把APP都上傳完了,突然發(fā)現(xiàn)一個會導(dǎo)致應(yīng)用閃退的小Bug,這時那個崩潰啊,明明不是很大的改動,難道我們還要再去重新去把各大應(yīng)用市場的版本再上傳更新一次?相信我,運營人員肯定會弄死你的??!
有問題,自然就會有解決問題的方案,因此我們就會想到如果在APP里內(nèi)嵌自動更新的功能,那么我們將可以省去很多麻煩,當然關(guān)于這方面功能的第三方SDK有很多。
好了,言歸正傳,今天我們自己來實現(xiàn)下關(guān)于APP自動更新。
流程其實并不復(fù)雜:當用戶打開APP的時候,我們讓APP去發(fā)送一個檢查版本的網(wǎng)絡(luò)請求,或者利用服務(wù)端向APP推送一個透傳消息來檢查APP的版本,如果當前APP版本比服務(wù)器上的舊,那么我們就提醒用戶進行下載更新APP,當然在特定的情況下,我們也可以強制的讓用戶去升級,當然這是很不友好的,盡可能的減少這樣的做法。
好了,來梳理下流程,首先既然是一個APP的更新,那么我們就需要去下載新的APP,然后我們需要一個通知來告訴用戶當前的下載進度,再來當APP安裝包下載完成后,我們需要去系統(tǒng)的安裝程序來對APP進行安裝更新。
知識點:
下載:異步HTTP請求文件下載,并監(jiān)聽當前下載進度(這里我采用了okhttp)
通知:Notification(具體用法請自行翻閱API文檔)
安裝:Intent (具體用法請自行翻閱API文檔)
來看下具體實現(xiàn)代碼:
我們需要一個后臺服務(wù)來支撐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服務(wù)
* 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來構(gòu)造
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) {
//當文件下載完成后回調(diào)
notifyMsg("溫馨提醒", "文件下載已完成", 100);
stopSelf();
}
@Override
public void inProgress(float progress, long total, int id) {
//progress*100為當前文件下載進度,total為文件大小
if ((int) (progress * 100) % 10 == 0) {
//避免頻繁刷新View,這里設(shè)置每下載10%提醒更新一次進度
notifyMsg("溫馨提醒", "文件正在下載..", (int) (progress * 100));
}
}
});
}
}
然后我們只需要在我們想要的更新APP的時候去調(diào)起這個服務(wù)即可,比如在系統(tǒng)設(shè)置里的"版本檢查"等
Intent intent = new Intent(mContext, DownloadService.class);
intent.putExtra("apkUrl", "APK下載地址");
startService(intent);
總結(jié)
這里我只是粗略演示本地自動更新APP的功能,在實際應(yīng)用中,我們應(yīng)該配合服務(wù)端來做,比如在用戶啟動APP的時候去比對版本號,如果版本號低于服務(wù)器的版本號,那么此時服務(wù)端應(yīng)該給客戶端一個透傳推送,這里的推送內(nèi)容應(yīng)該為新版本APP的下載地址,此時就可以根據(jù)該地址來下載新版APP了,當遇到重大更新,不再對老版本進行兼容的時候,可以強制用戶升級,這里的方案有很多,比如調(diào)用系統(tǒng)級對話框,讓用戶沒辦法取消等操作,這里就不做更多描述。以上就是這篇文章的全部內(nèi)容,希望對有需要的人能有所幫助。
相關(guān)文章
Notification與NotificationManager詳細介紹
在Android系統(tǒng)中,發(fā)一個狀態(tài)欄通知還是很方便的。下面我們就來看一下,怎么發(fā)送狀態(tài)欄通知,狀態(tài)欄通知又有哪些參數(shù)可以設(shè)置2012-11-11
Android自定義對話框Dialog的簡單實現(xiàn)
這篇文章主要為大家詳細介紹了Android自定義對話框Dialog的簡單實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
基于Android如何實現(xiàn)將數(shù)據(jù)庫保存到SD卡
有時候為了需要,會將數(shù)據(jù)庫保存到外部存儲或者SD卡中(對于這種情況可以通過加密數(shù)據(jù)來避免數(shù)據(jù)被破解),本文給大家分享Android如何實現(xiàn)將數(shù)據(jù)庫保存到SD卡,對android數(shù)據(jù)庫sd卡相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-12-12
詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android+OpenCV4.2.0環(huán)境配置詳解(Android studio)
這篇文章主要介紹了Android+OpenCV4.2.0環(huán)境配置詳解(Android studio),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

