How to call stopservice() method of Service class from the calling activity class

后端 未结 5 852
北海茫月
北海茫月 2020-11-30 06:19

I am trying to call my service class\'s stopService() method from my activity. But I dont know how to access stopservice method from my activity class. I have the below code

5条回答
  •  暖寄归人
    2020-11-30 07:13

    I actually used pretty much the same code as you above. My service registration in the manifest is the following

    
                
                    
                
            
    

    In the service class I created an according constant string identifying the service name like:

    public class MyService extends ForeGroundService {
        public static final String MY_SERVICE = "it.unibz.bluedroid.bluetooth.service.MY_SERVICE";
       ...
    }
    

    and from the according Activity I call it with

    startService(new Intent(MyService.MY_SERVICE));
    

    and stop it with

    stopService(new Intent(MyService.MY_SERVICE));
    

    It works perfectly. Try to check your configuration and if you don't find anything strange try to debug whether your stopService get's called properly.

提交回复
热议问题