preface

Now there are more and more Android all-in-one machines, such as advertising machine, query all-in-one machine, conference and teaching applications. The version iteration of these all-in-one machines is a troublesome thing, because it cannot be updated one by one, and the labor cost is large. Therefore, some time ago after I repeated debugging, completed a self-updating function of the demo, to share.

rendering

Here I do a demonstration, is to click check the update, the actual use can be through push, long connection and other ways to receive the update notification

Here is the use of polling download install application components, the principle and use of detailed see my other blog

www.jianshu.com/p/2651659ca…

Complete self-update, already a version higher

The principle of analytic

Mainly through register app replace radio installation, using polling after download and install components to download and install the new version of the silent apk, this process in the roof of the old version will be off, back to the desktop, but because has registered a radio, so replace installed radio will be called back, on the radio after the start of our app a since the update is complete.

That’s when people start asking questions

First, the implementation code in the broadcast

/ * * / * * * app version replace broadcast public static String UPDATE_ACTION = "android. Intent. Action. PACKAGE_REPLACED"; @Override public void onReceive(Context context, Intent Intent) {if (intent.getaction ().equals(UPDATE_ACTION)) {// get the packageName of the updated application. String packageName = intent.getData().getEncodedSchemeSpecificPart(); If (packageName. Equals (buildconfig.application_id)) { Restart the APP Intent intentToStart = context. GetPackageManager (). GetLaunchIntentForPackage (packageName); context.startActivity(intentToStart); }}}Copy the code

Don’t forget to configure it in the manifest file

<! - after a silent installation from start - > < receiver android: name = ". The receiver. The ReplaceAddRemoveBroadcastReceiver "> < intent - filter > < action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> </receiver>Copy the code

The source code

Github.com/Giftedcat/S… Give me a thumbs up if you like it