How to stop service by itself?

前端 未结 11 1126
独厮守ぢ
独厮守ぢ 2020-12-02 14:10

I start a service in an activity then I want the service to stop itself after a while.

I called stopSelf() in the service but it doesn\'t work.

How to make t

相关标签:
11条回答
  • 2020-12-02 14:59

    To let your service to stop itself.. create a BroadcastReceiver class.. In your service call your receiver like this..

    In service

    sendBroadcast(new Intent("MyReceiver"));
    

    In Broadcast Receiver

     public class MyReceiver extends BroadcastReceiver {
    
         @Override
         public void onReceive(Context context, Intent intent) {
    
             context.stopService(new Intent(context,NotificationService.class));
         }
     }
    

    Manifest file

        <receiver
            android:name="MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="MyReceiver"/>
            </intent-filter>
        </receiver>
    
    0 讨论(0)
  • 2020-12-02 15:01

    By calling stopSelf(), the service stops.

    Please make sure that no thread is running in the background which makes you feel that the service hasn't stopped.

    Add print statements within your thread.

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 15:01

    I just ran into the same issue. In my case, I have a singleton service manager that I use to communicate with the service. In the manager the service is started like this:

    context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE); 
    

    By removing Context.BIND_AUTO_CREATE as suggested by Alik Elzin, I've been able to stop the service using this.stopSelf() and to have onDestroy() called when doing so. This problem is that after that I wasn't able to restart the service from the manager using the command above.

    Finally I've fixed this by using a callback from the service that tells the manager to stop the service. This way the manager is always in charge when it comes to start/stop the service and everything seems to work fine. I don't know if there are any counter indications in doing it this way.

    The code is really simple. Create a callback in the service and set it in the manager like this in your connection class:

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            myService = ((MyService.LocalBinder)service).getService();
    
            myService.setCallback(new MyService.MyServiceCallback() {
                @Override
                public void onStop() {
                    stopService();
                }
            });
        }
    
        public void onServiceDisconnected(ComponentName className) {
            myService = null;
        }
    };
    

    and stop service:

    public void stopService()
    {
        if(mServiceConnection != null){
            try {
                mContext.unbindService(mServiceConnection);
            } catch (Exception e) {}
        }
    
         mContext.stopService(new Intent(mContext, BleDiscoveryService.class));
    }
    

    In the service, simply call myCallback.onStop() when you need to stop it.

    0 讨论(0)
  • 2020-12-02 15:01

    if you use separate Thread in your service, after stopping service by calling stopSelf() or stopService() the Thread keeps running. if u want to stop Thread u should call Thread.interrupted() in the Thread(it might cause an Exception if Thread is already sleeping)

    0 讨论(0)
  • 2020-12-02 15:07

    since you didnt publish your code, i cant know exactly what you are doing, but you must declare WHAT you are stopping:

    this.stopSelf();
    

    as in:

    public class BatchUploadGpsData extends Service {
        @Override
        public void onCreate() {
            Log.d("testingStopSelf", "here i am, rockin like a hurricane.   onCreate service");
            this.stopSelf();
        }
    
    0 讨论(0)
提交回复
热议问题