卸载应用程序时终止服务

Kill service when app is uninstalled

我有一个设置服务并启动 SettingContentObserver 以侦听音量、WiFi 和 GPS 的变化的应用程序。您可以选择是要启动服务还是停止服务。该服务是 START_STICKY,因此如果应用程序关闭它不会被杀死。

问题是:如果我在不终止进程的情况下卸载我的应用程序,我该怎么做才能终止它?

这是服务:

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();
    }
}

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();
        }

    }
}

在我的 main_activity 我有 startService(myService) 和 stopService(myService)

一旦您的应用程序被卸载,您的服务将自动终止。 也就是说,如果您的应用程序已卸载,则无法注册“卸载操作”。您只能接收来自其他软件包的卸载广播。