Android如何實現(xiàn)APP自動更新
先來看看要實現(xiàn)的效果圖:
對于安卓用戶來說,手機應(yīng)用市場說滿天飛可是一點都不夸張,比如小米,魅族,百度,360,機鋒,應(yīng)用寶等等,當(dāng)我們想上線一款新版本APP時,先不說渠道打包的麻煩,單純指上傳APP到各大應(yīng)用市場的工作量就已經(jīng)很大了,好不容易我們把APP都上傳完了,突然發(fā)現(xiàn)一個會導(dǎo)致應(yīng)用閃退的小Bug,這時那個崩潰啊,明明不是很大的改動,難道我們還要再去重新去把各大應(yīng)用市場的版本再上傳更新一次?相信我,運營人員肯定會弄死你的!!
有問題,自然就會有解決問題的方案,因此我們就會想到如果在APP里內(nèi)嵌自動更新的功能,那么我們將可以省去很多麻煩,當(dāng)然關(guān)于這方面功能的第三方SDK有很多。
好了,言歸正傳,今天我們自己來實現(xiàn)下關(guān)于APP自動更新。
流程其實并不復(fù)雜:當(dāng)用戶打開APP的時候,我們讓APP去發(fā)送一個檢查版本的網(wǎng)絡(luò)請求,或者利用服務(wù)端向APP推送一個透傳消息來檢查APP的版本,如果當(dāng)前APP版本比服務(wù)器上的舊,那么我們就提醒用戶進行下載更新APP,當(dāng)然在特定的情況下,我們也可以強制的讓用戶去升級,當(dāng)然這是很不友好的,盡可能的減少這樣的做法。
好了,來梳理下流程,首先既然是一個APP的更新,那么我們就需要去下載新的APP,然后我們需要一個通知來告訴用戶當(dāng)前的下載進度,再來當(dāng)APP安裝包下載完成后,我們需要去系統(tǒng)的安裝程序來對APP進行安裝更新。
知識點:
下載:異步HTTP請求文件下載,并監(jiān)聽當(dāng)前下載進度(這里我采用了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) { //當(dāng)文件下載完成后回調(diào) notifyMsg("溫馨提醒", "文件下載已完成", 100); stopSelf(); } @Override public void inProgress(float progress, long total, int id) { //progress*100為當(dāng)前文件下載進度,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了,當(dāng)遇到重大更新,不再對老版本進行兼容的時候,可以強制用戶升級,這里的方案有很多,比如調(diào)用系統(tǒng)級對話框,讓用戶沒辦法取消等操作,這里就不做更多描述。以上就是這篇文章的全部內(nèi)容,希望對有需要的人能有所幫助。
相關(guān)文章
Mac OS下為Android Studio編譯FFmpeg解碼庫的詳細教程
這篇文章主要介紹了Mac OS下為Android Studio編譯FFmpeg解碼庫的詳細教程,包括NDK的配置和Android Studio的配置兩個部分的內(nèi)容,需要的朋友可以參考下2016-01-01Android 讀取文件內(nèi)容實現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10Android測量每秒幀數(shù)Frames Per Second (FPS)的方法
這篇文章主要介紹了Android測量每秒幀數(shù)Frames Per Second (FPS)的方法,涉及Android針對多媒體文件屬性操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android遞歸方式刪除某文件夾下的所有文件(.mp3文件等等)
以刪除為例,當(dāng)然,對于遍歷某文件夾下的所有文件均可用這個方法。如搜索.mp3文件等,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Android獲取設(shè)備CPU核數(shù)、時鐘頻率以及內(nèi)存大小的方法
這篇文章主要介紹了Android獲取設(shè)備CPU核數(shù)、時鐘頻率以及內(nèi)存大小的方法,涉及Android針對系統(tǒng)硬件相關(guān)操作技巧,需要的朋友可以參考下2016-07-07