Android 调用 stopService 方法时服务不会停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2176375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:50:20  来源:igfitidea点击:

Service won't stop when stopService method is called

androidservice

提问by Donal Rafferty

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.

我目前有一个服务,当我启动它时运行良好,但是当我尝试使用 stopService 方法停止它时,它的 onDestroy 方法不会被调用。

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! ");

        }   
});

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.

我认为当使用 stopService 时它会调用服务的 onDestroy 方法吗?当我按下停止扫描按钮时,onDestroy()不会调用我的服务中的方法。

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

还有什么我应该停止服务的遗漏吗?

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

编辑:onServiceConnected()在 stopService 运行而不是运行时添加获取调用onServiceDisconnected(),为什么会发生这种情况?

EDIT:To add more info regards Binding

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

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

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

Here is the code for that method:

这是该方法的代码:

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

So I presume that the unbinding is not my problem?

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

回答by CommonsWare

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.

我猜你有一个方法调用releaseBind()意味着你之前调用bindService()过这个服务并且releaseBind()正在调用unbindService(). 如果我的猜测不正确,请忽略此答案。



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.

在所有bindService()呼叫都有相应的unbindService()呼叫后,服务将关闭。如果没有绑定客户端,那么stopService()当且仅当有人调用startService()该服务时,该服务也将需要。

So, there are a few possibilities here:

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

  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
  1. 您仍然有绑定的客户端(例如,其他活动),在这种情况下,您无法停止服务,直到它们解除绑定
  2. 由于unbindService()stopService()都是异步的,因此时间可能会出问题,在这种情况下,如果stopService()ServiceConnection'sonServiceDisconnected()方法调用,您可能会获得更好的运气

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).

此外,请记住,服务被销毁的确切时间取决于 Android,可能不会立即发生。因此,例如,如果您依赖于onDestroy()使您的服务停止正在完成的某些工作,请考虑为此使用另一个触发器(例如,活动stopDoingStuff()通过服务绑定器接口调用方法)。

回答by JRL

Are all your bindings closed?

你所有的绑定都关闭了吗?

A service can be used in two ways.The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed

可以通过两种方式使用服务。这两种模式并不是完全分开的。您可以绑定到使用 startService() 启动的服务。例如,可以通过使用标识要播放的音乐的 Intent 对象调用 startService() 来启动背景音乐服务。只有稍后,可能当用户想要对播放器进行某种控制或获取有关当前歌曲的信息时,活动才会通过调用 bindService() 建立与服务的连接。在这种情况下, stopService() 不会真正停止服务,直到最后一个绑定关闭

.

.

回答by dondondon

hai guys sorry for the late answer but as far as i know i have successfull stop the service in this code: you may check a linkhere.

伙计们很抱歉迟到的答案,但据我所知,我已成功停止此代码中的服务:您可以在此处查看链接

public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
  Log.d(TAG, "onClick: starting srvice");
  startService(new Intent(this, MyService.class));
  break;
case R.id.buttonStop:
  Log.d(TAG, "onClick: stopping srvice");
  stopService(new Intent(this, MyService.class));
  break;
}
}

and in services class:

在服务类中:

    public class MyService extends Service {
       private static final String TAG = "MyService";


@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");


}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");

}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

}
}