2017-06-09 144 views
0

我有一個應用程序設置服務並啓動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)

回答

0

您的服務將自動被打死,一旦你的應用程序被卸載。 此外,如果卸載的軟件包是您的,則無法註冊任何「卸載」操作。您只能接收來自其他軟件包的卸載廣播。

+0

Ooo。我怎樣才能看到我的服務?我正在使用Androis Studio 2.2.3及其虛擬設備 – Tostadora

+0

,您可以使用adb ...命令是:adb shell dumpsys活動服務 – JacksOnF1re

相關問題