Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Google Play - Can an App Update While it Is Running

I am building an app that is designed to run continuously. Furthermore, the user is locked into the app (via the pinning feature) when it is being used. When the app has not detected user interaction for a while, it unpins itself, calls the Android dream service, and displays a screensaver of sorts. When the user taps the device, it 'wakes up', goes to the main screen, and repins itself.

I need my app to auto-update. However, given the circumstances, I have had difficulty in doing so. It simply does not seem to update, or according to one of my colleagues, updated but closed the app.

Is there a way for the app to detect, download, and install an update while running, and then, if necessary, relaunch itself? What would be the best approach?

Thanks much.

like image 624
KellyM Avatar asked Jul 05 '16 14:07

KellyM


1 Answers

Is there a way for the app to detect, download, and install an update while running

This is handled for you by google play store. An app is killed when it is going to be upgraded to a new version so the installation of the new version is smooth and data is not corrupted.

if necessary, relaunch itself?

If you want to keep running (or rather, restart the app) after an upgrade, yes you have to do some additional stuff.

A way that I use is this:
Put in manifest:

<receiver android:name=".receivers.AppUpgraded">
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
    </intent-filter>
</receiver>

public class AppUpgraded extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        // your app was just upgraded, restart stuff etc.
    }
}

Note there is also a action.PACKAGE_REPLACED which will trigger for ANY app that is upgraded. You're likely only interested in the upgrade of your own app, so use action.MY_PACKAGE_REPLACED.

like image 182
Tim Avatar answered Sep 20 '22 08:09

Tim