how to prevent service to run again if already running android

前端 未结 5 2130
猫巷女王i
猫巷女王i 2021-02-06 21:49

On click of a button I want to start service using method startService(new Intent(currentActivity.this,MyService.class)) but if service is running I don\'t want to

5条回答
  •  误落风尘
    2021-02-06 22:08

    Whenever we start any service from any activity , Android system calls the service's onStartCommand() method And If the service is not already running, the system first calls onCreate(), and then it calls onStartCommand().

    So mean to say is that android service start's only once in its lifecycle and keep it running till stopped.if any other client want to start it again then only onStartCommand() method will invoked all the time.

提交回复
热议问题