欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何實(shí)現(xiàn)APP自動(dòng)更新

 更新時(shí)間:2016年08月30日 14:49:08   作者:李晨瑋  
現(xiàn)在一般的android軟件都是需要不斷更新的,當(dāng)你打開某個(gè)app的時(shí)候,如果有新的版本,它會(huì)提示你有新版本需要更新。該小程序?qū)崿F(xiàn)的就是這個(gè)功能。有需要的朋友們可以參考借鑒。

先來看看要實(shí)現(xiàn)的效果圖:

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

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

好了,言歸正傳,今天我們自己來實(shí)現(xiàn)下關(guān)于APP自動(dòng)更新。

流程其實(shí)并不復(fù)雜:當(dāng)用戶打開APP的時(shí)候,我們讓APP去發(fā)送一個(gè)檢查版本的網(wǎng)絡(luò)請(qǐng)求,或者利用服務(wù)端向APP推送一個(gè)透?jìng)飨頇z查APP的版本,如果當(dāng)前APP版本比服務(wù)器上的舊,那么我們就提醒用戶進(jìn)行下載更新APP,當(dāng)然在特定的情況下,我們也可以強(qiáng)制的讓用戶去升級(jí),當(dāng)然這是很不友好的,盡可能的減少這樣的做法。

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

知識(shí)點(diǎn):

下載:異步HTTP請(qǐng)求文件下載,并監(jiān)聽當(dāng)前下載進(jìn)度(這里我采用了okhttp)

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

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

來看下具體實(shí)現(xiàn)代碼:

我們需要一個(gè)后臺(tái)服務(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;

/**
 * 自動(dòng)下載更新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) {
  //下載進(jìn)行中
  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)前文件下載進(jìn)度,total為文件大小
  if ((int) (progress * 100) % 10 == 0) {
   //避免頻繁刷新View,這里設(shè)置每下載10%提醒更新一次進(jìn)度
   notifyMsg("溫馨提醒", "文件正在下載..", (int) (progress * 100));
  }
  }
 });


 }
}

然后我們只需要在我們想要的更新APP的時(shí)候去調(diào)起這個(gè)服務(wù)即可,比如在系統(tǒng)設(shè)置里的"版本檢查"等

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

總結(jié)

這里我只是粗略演示本地自動(dòng)更新APP的功能,在實(shí)際應(yīng)用中,我們應(yīng)該配合服務(wù)端來做,比如在用戶啟動(dòng)APP的時(shí)候去比對(duì)版本號(hào),如果版本號(hào)低于服務(wù)器的版本號(hào),那么此時(shí)服務(wù)端應(yīng)該給客戶端一個(gè)透?jìng)魍扑?,這里的推送內(nèi)容應(yīng)該為新版本APP的下載地址,此時(shí)就可以根據(jù)該地址來下載新版APP了,當(dāng)遇到重大更新,不再對(duì)老版本進(jìn)行兼容的時(shí)候,可以強(qiáng)制用戶升級(jí),這里的方案有很多,比如調(diào)用系統(tǒng)級(jí)對(duì)話框,讓用戶沒辦法取消等操作,這里就不做更多描述。以上就是這篇文章的全部?jī)?nèi)容,希望對(duì)有需要的人能有所幫助。

相關(guān)文章

最新評(píng)論