Joke Collection Website - Blessing messages - How to start and close Service in Android and explanation of functions
How to start and close Service in Android and explanation of functions
Startup:
//First register the service
lt; service
android: name="com.wangdeyu.service.MusicService "
android:exported="false" gt;
lt;intent-filtergt;
lt;action android:name="com.abc" / gt;
lt;/intent-filtergt;
lt;/servicegt;
//Start service
Intent service=new Intent(" com.abc");
startService(service);
//Service life cycle
①onCreate() Create Service
②onStart(Intent intent, int startId) Start Service
③onDestroy() Destroy Service
//Close Service
stopService(service);
Service has fewer life cycle methods than Activity, only onCreate, onStart, and onDestroy
We have two ways to start a Service, and their impact on the Service life cycle is different.
1 Through startService, it is the above type
Service will go through onCreate --gt; when onStart
stopService, it will directly onDestroy
If the caller exits directly without calling stopService, the Service will always run in the background.
The caller can still stopService the next time he gets up.
2 Through bindService
Service will only run onCreate. At this time, the caller and Service are bound together
If the caller exits, Srevice will call onUnbind --gt;onDestroyed
The so-called binding together means life or death.
Note: The onCreate method of Service will only be called once.
That is, no matter how many times you startService and bindService, Service will only be created once.
If it is bind first, then the onStart method of Service will be run directly when starting.
If it is start first, then the onBind method will be run directly when bind. If you bind first, you can't stop it.
You can only UnbindService first, and then StopService, so there is a difference between starting or binding first.
Services in Android are similar to services in Windows. Services generally do not have user operation interfaces. They run in the system and are not easily discovered by users. They can be used to develop programs such as monitoring.
The service cannot run by itself and needs to be started by calling the Context.startService() or Context.bindService() method.
Both methods can start the Service, but their usage scenarios are different. Use the startService() method to enable the service. There is no relationship between the caller and the service.
Even if the caller exits, the service is still running. Use the bindService() method to enable the service. The caller and the service are bound together. Once the caller exits, the service will be terminated. This has the characteristic of "doing not want to live at the same time, but must die at the same time".
If you plan to use the Context.startService() method to start the service, when the service has not been created, the system will first call the onCreate() method of the service,
and then call the onStart() method. . If the service has been created before calling the startService() method, calling the startService() method multiple times will not result in multiple creations of the service,
but will result in multiple calls to the onStart() method. A service started using the startService() method can only call the Context.stopService() method to end the service. When the service ends, the onDestroy() method will be called.
If you plan to use the Context.bindService() method to start the service, when the service has not been created, the system will first call the onCreate() method of the service,
and then call the onBind() method . At this time, the caller and the service are bound together. If the caller exits, the system will first call the onUnbind() method of the service,
and then call the onDestroy() method. If the service has been bound before calling the bindService() method,
Calling the bindService() method multiple times will not result in multiple creations and bindings of the service (that is, the onCreate() and onBind() methods and will not be called multiple times).
If the caller wants to unbind from the binding service, he can call the unbindService() method. Calling this method will also cause the system to call the service's onUnbind()-->onDestroy() method.
- Related articles
- User privacy policy
- Mid-Autumn Festival SMS greetings daquan
- Funny and humorous copywriting in friends circle
- Luzhou male teacher harassed girls and was arrested! What should girls do in this situation?
- Mini mobile phone is super small (easy to carry and enjoy communication anytime, anywhere)
- How to set hidden audience information in Aauto's faster live broadcast?
- How to turn off Tencent video push notification in Apple's mobile phone?
- Ivy loves to be super fond of not responding.
- There is a strange number in Xiaomi SMS synchronization management.
- Where is the SMS interception function set?