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

后端 未结 5 830
北海茫月
北海茫月 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 06:48

    In fact to stopping the service we must use the method stopService() and you are doing in right way:

    Start service:

      Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
      startService(myService);
    

    Stop service:

      Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
      stopService(myService);
    

    if you call stopService(), then the method onDestroy() in the service is called (NOT the stopService() method):

      @Override
        public void onDestroy() {
          timer.cancel();
          task.cancel();    
            Log.i(TAG, "onCreate() , service stopped...");
        }
    

    you must implement the onDestroy() method!.

    Here is a complete example including how to start/stop the service.

    0 讨论(0)
  • 2020-11-30 06:48

    That looks like it should stop the service when you uncheck the checkbox. Are there any exceptions in the log? stopService returns a boolean indicating whether or not it was able to stop the service.

    If you are starting your service by Intents, then you may want to extend IntentService instead of Service. That class will stop the service on its own when it has no more work to do.

    AutoService

    class AutoService extends IntentService {
         private static final String TAG = "AutoService";
         private Timer timer;    
         private TimerTask task;
    
         public onCreate() {
              timer = new Timer();
              timer = new TimerTask() {
                public void run() 
                {
                    System.out.println("done");
                }
              }
         }
    
         protected void onHandleIntent(Intent i) {
            Log.d(TAG, "onHandleIntent");     
    
            int delay = 5000; // delay for 5 sec.
            int period = 5000; // repeat every sec.
    
    
            timer.scheduleAtFixedRate(timerTask, delay, period);
         }
    
         public boolean stopService(Intent name) {
            // TODO Auto-generated method stub
            timer.cancel();
            task.cancel();
            return super.stopService(name);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 06:53

    @Juri

    If you add IntentFilters for your service, you are saying you want to expose your service to other applications, then it may be stopped unexpectedly by other applications.

    0 讨论(0)
  • 2020-11-30 07:03

    In Kotlin you can do this...

    Service:

    class MyService : Service() {
        init {
            instance = this
        }
    
        companion object {
            lateinit var instance: MyService
    
            fun terminateService() {
                instance.stopSelf()
            }
        }
    }
    

    In your activity (or anywhere in your app for that matter):

    btn_terminate_service.setOnClickListener {
        MyService.terminateService()
    }
    

    Note: If you have any pending intents showing a notification in Android's status bar, you may want to terminate that as well.

    0 讨论(0)
  • 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

    <service android:name=".service.MyService" android:enabled="true">
                <intent-filter android:label="@string/menuItemStartService" >
                    <action android:name="it.unibz.bluedroid.bluetooth.service.MY_SERVICE"/>
                </intent-filter>
            </service>
    

    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.

    0 讨论(0)
提交回复
热议问题