当stopService方法被调用服务不会停止方法、stopService

2023-09-11 12:19:48 作者:汪叽.

目前,我有我的时候开始,但是当我尝试使用stopService方法的的onDestroy方法不会被调用,以阻止它的运行良好的服务。

I currently have a Service that runs fine when I start it but when I try to stop it using the stopService method its onDestroy method doesn't get called.

下面是code我用它来尝试停止服务

Here is the code I use to try to stop the Service

   stop_Scan_Button = (Button)findViewById(R.id.stopScanButton);

    stop_Scan_Button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Log.d("DEBUGSERVICE", "Stop Button pressed");
            Intent service = new Intent(CiceroEngine. CICERO_SERVICE);
            releaseBind();
            Log.d("Stop_Scan_Button", "Service: " + service.toString());
            stopService(service);
            Log.d("Stop_Scan_Button", "Service should stop! ");

            }   
    });

难道我就在想,当stopService使用它调用服务的方法的onDestroy?当我preSS我停止扫描按钮在我服务的的onDestroy()方法不被调用。

还有什么我失踪,我应该把在停止服务?

Is there anything else I am missing that I should put in to stop the service?

编辑:添加 onServiceConnected()时stopService运行,而不是被称之为 onServiceDisconnected(),为什么会这是这样吗?

to add onServiceConnected() gets called when stopService is run instead of onServiceDisconnected(), why would that be happening?

编辑:要添加更多的信息关于绑定

我叫bindService在OnCreate()方法,然后我有releaseBind()方法取消绑定服务。

I call bindService in the onCreate() method and I then have the releaseBind() method unbind the Service.

下面是$ C $下这个方法:

Here is the code for that method:

 public void releaseBind(){
    unbindService(this);
  }

所以我presume的解除绑定是不是我的问题?

So I presume that the unbinding is not my problem?

推荐答案

我会猜,有一个呼吁 releaseBind方法()你的意思是你previously名为 bindService()在这个服务和 releaseBind()呼吁 unbindService( )。如果我的猜测是不正确,请忽略这个答案。

I am going to guess that your having a method call for releaseBind() means that you previously called bindService() on this service and that releaseBind() is calling unbindService(). If my guess is incorrect, please ignore this answer.

一个服务将关闭,毕竟 bindService()通话有其相应的 unbindService()通话。如果没有绑定的客户,那么服务也需要 stopService()当且仅当有人叫 startService()上的服务。

A service will shut down after all bindService() calls have had their corresponding unbindService() calls. If there are no bound clients, then the service will also need stopService() if and only if somebody called startService() on the service.

那么,有几个可能性:

您仍然绑定客户端(例如,其他活动),在这种情况下,直到他们解除你不能停止服务 由于两个 unbindService() stopService()是异步的,一些可能与时机将失控,在这种情况下,你可能会得到更好的运气,如果你调用 stopService() ServiceConnection onServiceDisconnected()方法 You still have bound clients (e.g., other activities), in which case you cannot stop the service until they unbind Since both unbindService() and stopService() are asynchronous, something might be going haywire with the timing, in which case you may get better luck if you call stopService() from your ServiceConnection's onServiceDisconnected() method

另外,要记住,被摧毁的服务的确切时间可达Android和可能不会立竿见影。因此,举例来说,如果你是依赖于的onDestroy()来使你的服务,停止正在做一些工作,考虑使用其他触发器的(例如,活动要求一个 stopDoingStuff()通过服务粘结剂接口方法)。

Also, bear in mind that the exact timing of the service being destroyed is up to Android and may not be immediate. So, for example, if you are relying upon onDestroy() to cause your service to stop some work that is being done, consider using another trigger for that (e.g., activity calling a stopDoingStuff() method through the service binder interface).