Following the previous article, the use of Android–service, let's continue to look at two ways to start service
The first kind of startservice
。
Run the code and know the following conclusions:
- We understand that its start-up cycle is Oncreate->onstartcommand,
- After exiting the app, the service process in the background still exists and is not destroyed
- When I clicked on multiple StartService, I clicked 3 times and you found that the OnCreate method was created only once.
- StopService,
The second kind of bindservice
Run the code to learn the following conclusions:
- Click Bindservice, Life cycle oncreat->onserviceconnected
- Exits the current application, throws an exception, and shuts down the service,
- Click Unbindservice,
Attention
StartService and Bindservice buttons are all clicked
Then we can't turn the service off when we hit StopService or Unbindservice.
Need to Unbindservice first, then StopService.
Android--Service two boot mode StartService and Bindservice