调用 stopService 方法时服务不会停止 [英] Service won't stop when stopService method is called

查看:34
本文介绍了调用 stopService 方法时服务不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个服务,当我启动它时运行良好,但是当我尝试使用 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.

这是我用来尝试停止服务的代码

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

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

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 时它会调用 Service 的 onDestroy 方法是正确的吗?当我按下停止扫描按钮时,我的服务中的 onDestroy() 方法未被调用.

Am I right in thinking that when stopService is used it calls the onDestroy method of the Service? When I press my stop scan button the onDestroy() method in my Service is not called.

还有什么需要我来停止服务的吗?

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?

添加更多关于绑定的信息

我在 onCreate() 方法中调用了 bindService,然后我让 releaseBind() 方法解除了服务的绑定.

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

这是该方法的代码:

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

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

So I presume that the unbinding is not my problem?

推荐答案

我猜你对 releaseBind() 的方法调用意味着你之前调用了 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() 调用后,服务将关闭.如果没有绑定的客户端,则当且仅当有人在服务上调用了 startService() 时,该服务还需要 stopService().

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.

所以,这里有几种可能性:

So, there are a few possibilities here:

  1. 您仍然有绑定的客户端(例如,其他活动),在这种情况下,您无法停止服务,直到它们解除绑定
  2. 由于 unbindService()stopService() 都是异步的,所以时间可能会出问题,在这种情况下,如果你调用 stopService() 来自 ServiceConnectiononServiceDisconnected() 方法
  1. You still have bound clients (e.g., other activities), in which case you cannot stop the service until they unbind
  2. 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).

这篇关于调用 stopService 方法时服务不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆