卸载应用程序时终止服务

时间:2017-06-09 14:47:21

标签: android service contentobserver

我有一个应用程序设置服务并启动一个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)

1 个答案:

答案 0 :(得分:0)

卸载应用后,您的服务将自动被杀死。 此外,如果卸载的软件包是您的,则无法在任何“卸载”操作上注册。您只能从其他软件包接收卸载广播。