简体   繁体   中英

How to stop my service?

Please also see Edit 2 (below)

I have extended NotificationListenerService and implemented several of its methods including the onDestroy() method (but not the onBind() or onStartCommand() method, as I don't need them, as far as I know). However when I call selfStop() onDestroy() isn't called.

I know there are other links out there about calling selfStop() , but I haven't found any solution for my problem.

My service is started by the system using the following code in the android-manifest :

<service android:name=".NotificationListener"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
</service>

Again, when I try to call stopSelf() in the onCreate() method (if certain conditions are met) the onDestroy() method isn't called...the service isn't destroyed.

It is constantly using RAM (I know it's running because I can see it under Settings -> Apps -> Running (on my smartphone))

Does anyone know why stopSelf() might not be executed/run? Do I have to do something in the onBind() method (eg change the returned constant)? Is it even possible to stop my service that extends NotificationListenerService , so that it isn't running before it's called again by the system? Do I have to call unbind() before calling stopSelf() ? If so then using which Context and ServiceConnection ?

Edit 1

Here is my code:

public class Core extends NotificationListenerService {

@Override
public void onCreate() {
    // TODO Auto-generated method stub      
    super.onCreate();
    Log.i("abc", "onCreateService");
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    //check main switch state:
    check = prefs.getBoolean("mainswitch", true);
    Toast.makeText(Core.this, Boolean.toString(check), Toast.LENGTH_SHORT).show();
    Log.i("abc", Boolean.toString(check));
    if(check == false){
        stopSelf();
    }
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.i("abc", "onDestroyService");
}
}

Edit 2

If I have problems destroying my service that extends the NotificationListenerService (see above) would it be smart to create a second service (eg MirrorClass) that basically mirrors the NotificationListenerService ? I would then just refer to that class and its methods from the actual service that extends NotificationListenerService . Presumably that would then be easier to destroy as I was the one that started it (unlike the service that extends NotificationListenerService that is started/called by the android-system itself)

Something like this:

public class Core extends NotificationListenerService {

@Override
public void onCreate() {
    // TODO Auto-generated method stub      
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    MirrorClass.mOnNotificationPosted(sbn) //This is the reference to the other class
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){}

Would really appreciate some guidance/help here, it's starting to do my head in, thanks

Try overrides onStartCommand() in your Service with START_NOT_STICKY flag:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_NOT_STICKY;
}

Create a new service that does all of the work for you, instead of doing the work in this service that extends NotificationListenerService . All you do then is use the startService() method once you want to do some work and presumably you will send some extras with that intent containing the posted notification. It is then very easy to stop that service (that does the work for you and doesn't extend NotificationListenerService ) using the stopSelf() method. Stopping the NotificationListenerService itself using the stopSelf() method doesn't work.

Place command

stopSelf();

to

public int onStartCommand(Intent intent, int flags, int startId) {

Make some variable for checking if service should be stopped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM