如何停止我的服务?我的

2023-09-07 22:04:52 作者:陪我沦陷

请参见编辑2(下)

我已经延长 NotificationListenerService 并实现它的许多方法,包括的onDestroy()办法(而不是 onBind() onStartCommand()方法,因为我并不需要它们,因为据我所知)。然而,当我打电话 selfStop() 的onDestroy()不叫。

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.

我知道有其他环节出有大约叫 selfStop(),但我还没有找到我的问题的任何解决方案。

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

我的服务是使用下面的code由系统启动 Android的清单

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>

同样,当我尝试调用 stopSelf()的onCreate()方法(如果某些条件满足)的的onDestroy()方法不叫......该服务不被破坏。

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.

这是经常使用的RAM(我知道它的运行,因为我可以看到它在设置 - >应用程序 - >我的智能手机上运行())

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

有谁知道为什么 stopSelf()可能不会被执行/运行?我必须做一些事情,在 onBind()方法(如改变返回常数)? 它甚至有可能停止我的服务扩展NotificationListenerService ,因此它没有运行之前,它再次被称为系统? 我一定要叫解除绑定()之前调用 stopSelf()?如果是的话,使用其中上下文 ServiceConnection

Does anyone know why stopSelf() might not be executed/run? Do I have to do something in the onBind() method (e.g. 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?

修改1

下面是我的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");
}
}

编辑2

如果我有问题,毁了我的服务,扩展了 NotificationListenerService (见上文)会是明智的创建第二个服务(如MirrorClass),基本上反映了 NotificationListenerService ?那么我只是指的是类和扩展 NotificationListenerService 实际服务的方法。 presumably那将是更容易破坏,因为我是启动它(一个不同于延伸服务 NotificationListenerService 已启动了/叫的是Android系统本身)

If I have problems destroying my service that extends the NotificationListenerService (see above) would it be smart to create a second service (e.g. 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)

事情是这样的:

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){}

真的AP preciate一些指导/有所帮助,它开始做我的头,谢谢

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

推荐答案

创建一个新的服务,只要不是在做这项服务扩展的工作都为你工作, NotificationListenerService 。所有你要做的则是使用 startService()方法,一旦你想要做一些工作,presumably你会送一些演员意图包含张贴通知。 它是那么很容易停止该服务(即是否为您的工作和不延 NotificationListenerService )使用 stopSelf()方法。 停止 NotificationListenerService 本身使用 stopSelf()方法是行不通的。

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.