Android 卸载应用程序时终止服务
我有一个应用程序,可以设置一项服务并启动一个设置ContentObserver来监听音量、WiFi和GPS的变化。您可以选择是启动服务还是停止服务。该服务具有启动粘性,因此在应用程序关闭时不会被终止 问题是:如果我在不终止进程的情况下卸载我的应用程序,我该怎么做才能终止它 这是服务:Android 卸载应用程序时终止服务,android,service,contentobserver,Android,Service,Contentobserver,我有一个应用程序,可以设置一项服务并启动一个设置ContentObserver来监听音量、WiFi和GPS的变化。您可以选择是启动服务还是停止服务。该服务具有启动粘性,因此在应用程序关闭时不会被终止 问题是:如果我在不终止进程的情况下卸载我的应用程序,我该怎么做才能终止它 这是服务: public class ListenerService extends Service { SettingsContentObserver mSettingsContentObserver; Co
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();
}
}
内容观察者
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();
}
}
}
在我的主要活动中,我有startService(myService)和stopService(myService)一旦卸载应用程序,您的服务将自动终止。
也就是说,如果您的应用程序被卸载,则无法注册“卸载操作”。您只能从其他软件包接收卸载广播。Ooo。我怎样才能看到我的服务?我正在使用Androis Studio 2.2.3及其虚拟设备您可以使用adb…命令是:adb shell dumpsys activity services