Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain a background service after android app update

Tags:

android

My background service performs the core functionality of my app and is first started when the UI is opened.

So when the app is updated on the play store, the service is killed but the UI may not be opened again(So I guess the Service does not start again too).This is bad for me as the service performs the core functionality of the app. How do I overcome this?

Any help is deeply appreciated

like image 583
abhilash poojary Avatar asked Jul 22 '15 18:07

abhilash poojary


1 Answers

Your service will be killed during upgrade. You need to catch upgrade event and start it again.

In Manifest.xml

<receiver android:name=".yourpackage.UpgradeReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            </intent-filter>
        </receiver>

In UpgradeReceiver.java

public class UpgradeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (!Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction()))
                return;

            // Start your service here. 
        } catch (Exception e) {
            Utils.handleException(e);
        }
    }
}
like image 96
thanhbinh84 Avatar answered Oct 30 '22 09:10

thanhbinh84