how can i stop my RECEIVE_BOOT_COMPLETED service

2019-04-17 08:44发布

seeing many questions about this but im unable to fix this.

I have this code

public class myBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "myBroadcastReceiver";
  @Override
  public void onReceive(Context context, Intent intent) {
     if(intent.getAction().equals(Consts.ANDROID_INTENT_ACTION_BOOT_COMPLEATE)){
    Intent newinIntent = new Intent(context, ServiceBootCompleated.class);
    context.startService(newinIntent);
     }
  }
}

It starts a Service and i can debug it using this line

android.os.Debug.waitForDebugger(); 

I see that return START_NOT_STICKY; is executed but still the service is visible as a "running" service in the Setttings>programs>Running Services

the onDestroy() is never called unless i stop it manually.
What do i have to do to stop it,
remove it from "Setttings>programs>Running Services " window?

2条回答
来,给爷笑一个
2楼-- · 2019-04-17 09:19

on completion of task, you have to do context.stopService() for stopping this type of unbound service.

Regards,
SSuman185

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-17 09:37

Once you have completed the work you wanted to do in the background call stopSelf()

Be sure that any real work you do in the Service is done as a background thread and not in onCreate or onStartCommand.

See http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle for more details on the Service Lifecycle.

Example:

public int onStartCommand(final Intent intent, final int flags, final int startId)
{       
    Thread thread = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
                    //do work
                stopSelf();
        }
    },"MyWorkerThread");
    thread.start();
    return Service.START_NOT_STICKY;
}
查看更多
登录 后发表回答