Android auto-updates from zero to single, and Android
Android auto-renewal from zero to single
Reprinted please indicate the source: http://blog.csdn.net/crazy1235/article/details/45584621
Automatic update Principle
When we release our applications, we will surely think of subsequent version updates. How should we update our programs?
The principle of updating APK is actually in the Comparison ProgramAndroidManifest. xmlInVersionCodeFor comparison, if the code of a later version is found, it indicates that a new version of the program is released. In this case, you can prompt the user "Please update the new version ".
Automatic Updates of umeng
Of course, developers can write a service on their own to read the versionCode value from the apk file on the server, and then perform comparative analysis. However, if the development time is tight, it is much more convenient to use a third-party tool. After all, what should we do if we have a wheel ???
Umeng automatic update
When you use umeng's Update Service, you need to upload the application to its server, and then integrate the apk of umeng's automatic update service to detect the application Version Information uploaded by developers, if you find that the server version is higher, you will be prompted to have a new application version, and then you can download the new version of the application for installation.
Umeng supports multiple update Methods:Automatic update, manual update, and silent update.
Preparations
First need to go to the official website download SDK, address: http://www.umeng.com/component_update
After downloading the SDK, you will see three folders: demo, jar package, and res resource files.
- Copy the files in libs to your project.Libs/armeabi/libbspatch. soFiles are used to support the incremental update function.
- Copy the res file to the project. umeng's resource files areUmeng _.
- Configure the AndroidManifest. xml file:
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permission android:name="android.permission.INTERNET"></uses-permission>
<Meta-data android: name = "UMENG_APPKEY" android: value = "Your APPKEY"/> <meta-data android: name = "UMENG_CHANNEL" android: value = "Channel ID"/>
<service android:name="com.umeng.update.net.DownloadingService" android:process=":DownloadingService" ></service><activity android:name="com.umeng.update.UpdateDialogActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" ></activity>
OK. The preparation is complete.
Automatic update
Automatic update means that after the application is opened, the system determines the network and actively checks for version updates. If there are any updates, the system prompts the user to download the updates.
The automatic update code is concise:
UmengUpdateAgent.update(this);
In this way, a dialog box compiled by umeng is displayed:
However, this interface is a bit ugly.
Next, we will add a listener for updates, and then customize the pop-up box:
/*** Display the update dialog box ** @ param updateResponse * @ param context */UmengUpdateAgent. setDefault (); UmengUpdateAgent. setUpdateOnlyWifi (false); UmengUpdateAgent. setUpdateAutoPopup (false); UmengUpdateAgent. setUpdateListener (new UmengUpdateListener () {@ Override public void onUpdateReturned (int statusCode, UpdateResponse updateResponse) {Toast. makeText (MainActivity. this, statusCode + "", 1000 ). show (); if (stat UsCode = UpdateStatus. Yes & updateResponse! = Null) {showUpdateDialog (updateResponse) ;}}); UpdateConfig. setDebug (true); UmengUpdateAgent. setRichNotification (true); UmengUpdateAgent. update (getApplicationContext ());
SetDefault ():Restore the default settings.If the update detection method is called more than once in the application, and the settings are different each time, you need to restore the default settings and then set other parameters.
SetUpdateOnlyWifi ():Check whether the setting is followed by a Wi-Fi Environment.
SetDeltaUpdate ():Incremental update Switch.
SetUpdateAutoPoup ():Update prompt switch. To handle the update callback, We need to disable the automatic pop-up prompt.
UmengUpdateListener ():Update callback class. There is a callback method.OnUpdateReturned (int upadteStatus, UpdateResponse upadteInfo), UpdateStatus is used to determine whether there is an update. upodateInfo encapsulates some updated content, including the version number and update log.
Private static void showUpdateDialog (final UpdateResponse updateResponse, final Context context) {AlertDialog. builder updateAlertDialog = new AlertDialog. builder (context); updateAlertDialog. setIcon (R. drawable. icon_app); updateAlertDialog. setTitle ("version update prompt"); String showMessage = "Latest Version:" + updateResponse. version + "\ n" + "Update log: \ n" + updateResponse. updateLog; updateAlertDialog. setMessage (showMessage); UpdateAlertDialog. setNegativeButton ("upgrade now", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {File file = UmengUpdateAgent. downloadedFile (context, updateResponse); if (null = file) {UmengUpdateAgent. startDownload (context, updateResponse);} else {// the latest version of APK that has been downloaded. Install UmengUpdateAgent directly. startInstall (context, file) ;}}); updateAlertDialo G. setNeutralButton ("ignore this version", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {UmengUpdateAgent. ignoreUpdate (context, updateResponse) ;}}); updateAlertDialog. setPositiveButton ("cancel", null); if (! (Activity) context). isFinishing () updateAlertDialog. show ();}
If you need to display the update progress and respond accordingly when the APK download starts, starts, ends, or fails, you need to set the download callback interface:
UmengUpdateAgent.setDownloadListener(new UmengDownloadListener(){ @Override public void OnDownloadStart() { Toast.makeText(mContext, "download start" , Toast.LENGTH_SHORT).show(); } @Override public void OnDownloadUpdate(int progress) { Toast.makeText(mContext, "download progress : " + progress + "%" , Toast.LENGTH_SHORT).show(); } @Override public void OnDownloadEnd(int result, String file) { Toast.makeText(mContext, "download file path : " + file , Toast.LENGTH_SHORT).show(); } });
Manual update.
In the settings of many applications, we can see that"Detect new versions. Manual updates are basically the same as automatic updates. The difference is thatRegardless of the network conditions, whether or not you ignore updates of this version, the system checks for updates.
UmengUpdateAgent.setDefault();UmengUpdateAgent.setUpdateAutoPopup(false);UmengUpdateAgent.forceUpdate(MainActivity.this);UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() { @Override public void onUpdateReturned(int statusCode, UpdateResponse updateResponse) { Toast.makeText(MainActivity.this, statusCode + "", 1000).show(); if (statusCode == UpdateStatus.Yes && updateResponse != null) { showUpdateDialog(updateResponse); } }});
If there is a version, how to operate it depends on how you write it in the callback function. It is basically the same as automatic update.
Silent download
Application Scenario: when a user enters the home page, if the user is in wifi and the background detects an update, the user will automatically download it. After the download is complete, the user will be notified.
I believe many applications are doing this.
The preceding settings are the same.
UmengUpdateAgent.silentUpdate(this);
Summary
Demo
Thank you for your support. please correct me if you have any questions.
Thank you ~~~