androidservicecontentobserver

Kill service when app is uninstalled


I have an app that sets a service and starts a SettingContentObserver to listen to changes in the volume, WiFi and GPS. You can choose if you want to start the service or stop it. The service is START_STICKY so it doesn't get killed if the app is closed.

The question is: if I unistall my app without killing the process, what can I do to kill it?

This is the service:

public class ListenerService extends Service {
    SettingsContentObserver mSettingsContentObserver;
    Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        mSettingsContentObserver = new SettingsContentObserver(context, new Handler());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        this.getApplicationContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mSettingsContentObserver);
        Toast.makeText(this, "Service starting onStartCommand", Toast.LENGTH_SHORT).show();

        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
        getContentResolver().unregisterContentObserver(mSettingsContentObserver);
        stopSelf();
    }
}

The ContentObserver

public class SettingsContentObserver extends ContentObserver {
    int previousVolume;
    Context context;

    public SettingsContentObserver(Context c, Handler handler) {
        super(handler);
        context=c;

        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    public SettingsContentObserver(Handler handler) {
        super(handler);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        if (audio.getRingerMode() != AudioManager.RINGER_MODE_SILENT){
            Toast.makeText(context, "Se ha modificado el volumen", Toast.LENGTH_SHORT).show();
        }
        audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

        //In androidManifest --> android.permission.ACCESS_NETWORK_STATE
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (!mWifi.isConnected()){
            Toast.makeText(context, "Esta descontectado el WiFi", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context, "Esta conectado el WiFi", Toast.LENGTH_SHORT).show();
        }

    }
}

in my main_activity I have startService(myService) and stopService(myService)


Solution

  • Your service will automatically get killed, once your app is uninstalled. That said, there is no way to register on an "uninstall action", if your application is uninstalled. You can only receive uninstall broadcasts from other packages.