在应用中更新App版本

jopen 9年前
   <blockquote>     <p>欢迎Follow我的<a href="/misc/goto?guid=4959652813041333444" target="_blank">GitHub</a>, 关注我的<a href="/misc/goto?guid=4959652813131126148" target="_blank">简书</a>.</p>    </blockquote>    <p>在应用中, 为了提高用户体验, 会提供更新版本的功能. 那么如何实现呢? 我写了一个简单的Demo, 说明一下, 需要注意几个细节. 使用了Retrofit和Rx处理网络请求.</p>    <p>Github<a href="/misc/goto?guid=4959652813209103367" target="_blank">下载地址</a></p>    <div class="image-package" href="https://simg.open-open.com/show/bef33667ddb6b01419222a103e8ba887.png">    <img src="https://simg.open-open.com/show/bef33667ddb6b01419222a103e8ba887.png" width="700" height="550.2777777777777" data-original-src="https://simg.open-open.com/show/807837f2091a3d4a52b647cf71ad51a2.png" />     <br />     <div class="image-caption">     更新     </div>    </div>    <h1>1. 逻辑</h1>    <p>访问服务器, 根据是否包含新版本, 判断是否需要更新.<br /> 下载Apk, 下载完成后, 自动安装, 高版本会覆盖低版本.</p>    <p>逻辑:</p>    <pre class="brush:java; toolbar: true; auto-links: false;">public class MainActivity extends AppCompatActivity {      private static final String APP_NAME = "Ped_android";     private static final String VERSION = "1.0.0";     private static final String INFO_NAME = "计步器";     private static final String STORE_APK = "chunyu_apk";      @Bind(R.id.main_b_install_apk) Button mBInstallApk;      private UpdateAppUtils.UpdateCallback mUpdateCallback; // 更新回调      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         ButterKnife.bind(this);          mUpdateCallback = new UpdateAppUtils.UpdateCallback() {             @Override public void onSuccess(UpdateInfo updateInfo) {                 Toast.makeText(MainActivity.this, "有更新", Toast.LENGTH_SHORT).show();                 UpdateAppUtils.downloadApk(MainActivity.this, updateInfo, INFO_NAME, STORE_APK);             }              @Override public void onError() {                 Toast.makeText(MainActivity.this, "无更新", Toast.LENGTH_SHORT).show();             }         };          mBInstallApk.setOnClickListener(new View.OnClickListener() {             @Override public void onClick(View v) {                 UpdateAppUtils.checkUpdate(APP_NAME, VERSION, mUpdateCallback);             }         });     } }</pre>    <p><code>UpdateAppUtils</code>是核心下载类. 输入App的代号, 版本号, 异步回调, 发送到服务器, 判断是否需要更新. 如果存在新版本, 则下载Apk, 并自动安装更新. </p>    <h1>2. 网络请求</h1>    <p>更新请求, 参数是App代号和当前版本号.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * 更新服务  * <p>  * Created by wangchenlong on 16/1/4.  */ public interface UpdateService {     String ENDPOINT = "http://www.chunyuyisheng.com";      // 获取个人信息     @GET("/cmsapi/app/update")     Observable<UpdateInfo> getUpdateInfo(             @Query("appName") String appName,             @Query("version") String version); }</pre>    <p>创建服务的工厂类.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * 创建Retrofit服务  * <p>  * Created by wangchenlong on 16/1/4.  */ public class ServiceFactory {     public static <T> T createServiceFrom(final Class<T> serviceClass, String endpoint) {         Retrofit adapter = new Retrofit.Builder()                 .baseUrl(endpoint)                 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 添加Rx适配器                 .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器                 .build();         return adapter.create(serviceClass);     } }</pre>    <p>更新信息的Json类.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * 更新信息(JSON)  * <p>  * Created by wangchenlong on 16/1/4.  */ public class UpdateInfo {     public Data data; // 信息     public Integer error_code; // 错误代码     public String error_msg; // 错误信息      public static class Data {         public String curVersion; // 当前版本         public String appURL; // 下载地址         public String description; // 描述         public String minVersion; // 最低版本         public String appName; // 应用名称     }      @Override public String toString() {         return "当前版本: " + data.curVersion + ", 下载地址: " + data.appURL + ", 描述信息: " + data.description                 + ", 最低版本: " + data.minVersion + ", 应用代称: " + data.appName                 + ", 错误代码: " + error_code + ", 错误信息: " + error_msg;     } }</pre>    <h1>3. 请求和下载</h1>    <p>更新库的主类, 包含<code>检查更新(checkUpdate)</code>和<code>下载Apk(downloadApk)</code>两个重要方法.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * 更新管理器  * <p>  * Created by wangchenlong on 16/1/6.  */ @SuppressWarnings("unused") public class UpdateAppUtils {      @SuppressWarnings("unused")     private static final String TAG = "DEBUG-WCL: " + UpdateAppUtils.class.getSimpleName();      /**      * 检查更新      */     @SuppressWarnings("unused")     public static void checkUpdate(String appCode, String curVersion, UpdateCallback updateCallback) {         UpdateService updateService =                 ServiceFactory.createServiceFrom(UpdateService.class, UpdateService.ENDPOINT);          updateService.getUpdateInfo(appCode, curVersion)                 .subscribeOn(Schedulers.newThread())                 .observeOn(AndroidSchedulers.mainThread())                 .subscribe(updateInfo -> onNext(updateInfo, updateCallback),                         throwable -> onError(throwable, updateCallback));     }      // 显示信息     private static void onNext(UpdateInfo updateInfo, UpdateCallback updateCallback) {         Log.e(TAG, "返回数据: " + updateInfo.toString());         if (updateInfo.error_code != 0 || updateInfo.data == null ||                 updateInfo.data.appURL == null) {             updateCallback.onError(); // 失败         } else {             updateCallback.onSuccess(updateInfo);         }     }      // 错误信息     private static void onError(Throwable throwable, UpdateCallback updateCallback) {         updateCallback.onError();     }      /**      * 下载Apk, 并设置Apk地址,      * 默认位置: /storage/sdcard0/Download      *      * @param context    上下文      * @param updateInfo 更新信息      * @param infoName   通知名称      * @param storeApk   存储的Apk      */     @SuppressWarnings("unused")     public static void downloadApk(             Context context, UpdateInfo updateInfo,             String infoName, String storeApk     ) {         if (!isDownloadManagerAvailable()) {             return;         }          String description = updateInfo.data.description;         String appUrl = updateInfo.data.appURL;          if (appUrl == null || appUrl.isEmpty()) {             Log.e(TAG, "请填写\"App下载地址\"");             return;         }          appUrl = appUrl.trim(); // 去掉首尾空格          if (!appUrl.startsWith("http")) {             appUrl = "http://" + appUrl; // 添加Http信息         }          Log.e(TAG, "appUrl: " + appUrl);          DownloadManager.Request request;         try {             request = new DownloadManager.Request(Uri.parse(appUrl));         } catch (Exception e) {             e.printStackTrace();             return;         }         request.setTitle(infoName);         request.setDescription(description);         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {             request.allowScanningByMediaScanner();             request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);         }         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, storeApk);          Context appContext = context.getApplicationContext();         DownloadManager manager = (DownloadManager)                 appContext.getSystemService(Context.DOWNLOAD_SERVICE);          // 存储下载Key         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(appContext);         sp.edit().putLong(PrefsConsts.DOWNLOAD_APK_ID_PREFS, manager.enqueue(request)).apply();     }      // 最小版本号大于9     private static boolean isDownloadManagerAvailable() {         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;     }      // 错误回调     public interface UpdateCallback {         void onSuccess(UpdateInfo updateInfo);          void onError();     } }</pre>    <p>检查更新: 创建服务, 在新线程中发送请求, 在主线程中接收数据, 判断成功和失败.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    /**      * 检查更新      */     @SuppressWarnings("unused")     public static void checkUpdate(String appCode, String curVersion, UpdateCallback updateCallback) {         UpdateService updateService =                 ServiceFactory.createServiceFrom(UpdateService.class, UpdateService.ENDPOINT);          updateService.getUpdateInfo(appCode, curVersion)                 .subscribeOn(Schedulers.newThread())                 .observeOn(AndroidSchedulers.mainThread())                 .subscribe(updateInfo -> onNext(updateInfo, updateCallback),                         throwable -> onError(throwable, updateCallback));     }</pre>    <p>下载Apk: 转换和解析Url, 设置通知信息和存储位置, 存储下载Id, 自动安装更新.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    /**      * 下载Apk, 并设置Apk地址,      * 默认位置: /storage/sdcard0/Download      *      * @param context    上下文      * @param updateInfo 更新信息      * @param infoName   通知名称      * @param storeApk   存储的Apk      */     @SuppressWarnings("unused")     public static void downloadApk(             Context context, UpdateInfo updateInfo,             String infoName, String storeApk     ) {         if (!isDownloadManagerAvailable()) {             return;         }          String description = updateInfo.data.description;         String appUrl = updateInfo.data.appURL;          if (appUrl == null || appUrl.isEmpty()) {             Log.e(TAG, "请填写\"App下载地址\"");             return;         }          appUrl = appUrl.trim(); // 去掉首尾空格          if (!appUrl.startsWith("http")) {             appUrl = "http://" + appUrl; // 添加Http信息         }          Log.e(TAG, "appUrl: " + appUrl);          DownloadManager.Request request;         try {             request = new DownloadManager.Request(Uri.parse(appUrl));         } catch (Exception e) {             e.printStackTrace();             return;         }         request.setTitle(infoName);         request.setDescription(description);         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {             request.allowScanningByMediaScanner();             request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);         }         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, storeApk);          Context appContext = context.getApplicationContext();         DownloadManager manager = (DownloadManager)                 appContext.getSystemService(Context.DOWNLOAD_SERVICE);          // 存储下载Key         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(appContext);         sp.edit().putLong(PrefsConsts.DOWNLOAD_APK_ID_PREFS, manager.enqueue(request)).apply();     }</pre>    <blockquote>     <p>使用<code>DownloadManager</code>下载文件是Android的推荐方式.<br /> 存储<code>下载Id(manager.enqueue(request))</code>是为了在安装应用时, 找到Apk.<br /> 默认存储地址<code>/storage/sdcard0/Download</code>.</p>    </blockquote>    <h1>4.自动安装</h1>    <p>注册广播接收器, 接收消息<code>ACTION_DOWNLOAD_COMPLETE</code>, 下载完成会发送广播. 获取下载文件的Uri, 进行匹配, 发送安装消息, 自动安装.</p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * 安装下载接收器  * <p>  * Created by wangchenlong on 16/1/5.  */ public class InstallReceiver extends BroadcastReceiver {      private static final String TAG =             "DEBUG-WCL: " + InstallReceiver.class.getSimpleName();      // 安装下载接收器     @Override public void onReceive(Context context, Intent intent) {         if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {             long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);             installApk(context, downloadApkId);         }     }      // 安装Apk     private void installApk(Context context, long downloadApkId) {         // 获取存储ID         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);         long id = sp.getLong(PrefsConsts.DOWNLOAD_APK_ID_PREFS, -1L);          if (downloadApkId == id) {             DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);             Intent install = new Intent(Intent.ACTION_VIEW);             Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);             if (downloadFileUri != null) {                 install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");                 install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                 context.startActivity(install);             } else {                 Log.e(TAG, "下载失败");             }         }     } }</pre>    <blockquote>     <p>安装本应用下载的Apk, 不安装其他Apk, 存储下载Id, 与广播Id进行匹配.<br /> 下载失败, 也会发送<code>下载完成(ACTION_DOWNLOAD_COMPLETE)</code>广播, Uri可能为空, 需要判断, 否则发生崩溃.</p>    </blockquote>    <p>OK, that's all! Enjoy It!</p>    <p>来自: <a href="/misc/goto?guid=4959652813293124879" rel="nofollow" target="_blank">http://www.jianshu.com/p/5a6501a934cb</a></p>