How to stop service immediately?

2019-08-25 12:42发布

In android development, I have service that started by Brodcastreceiver ,and it started and did its task properly , but when I try to stop the service from its class by using stopself(); it did not stop until performing its task three times and then its stop here is the code for the onStart service and onDestroy();:

@Override
    public void onStart(Intent intent, int startid) {
        player.start();
        SystemClock.sleep(30000);
        onDestroy();
    }

    @Override
    public void onDestroy() {
        player.stop();
    this.stopSelf();}   

and here is the code when I started the service from the BrodcastReceiver:

Intent intent =new Intent(context,RingService.class);
context.startService(intent);

Is there any way to make my service stop immediately once its finished its task by itself , or do I have to make the BrodcastRecevicer stop it?

any help or redirection to solve this issue will be completlly appreciated

regards

1条回答
看我几分像从前
2楼-- · 2019-08-25 13:30

Just call stopSelf() at the end of onStart instead of the onDestroy() call and remove the stopSelf() call from onDestroy().

查看更多
登录 后发表回答