Service Details analysis, service details
1. What is the difference between Service start and bind status?
A> start: the start method is called multiple times. Only onCreate is called for the first time, and only onStartCommand and onStart are called after that,
Only the first call of bind will start onCreate and onBind.
B> unbind: Call stopService to unbind a bind after start. You can call it multiple times. Call unbindService to unbind a bind after start,
Only one call is allowed. Multiple calls will report java. lang. IllegalArgumentException: Service not registered.
C> life cycle: the Service started by start. After its components are destroyed, the Service still exists. The service started by bind, in the dependent
After the component is destroyed, the Service will also be destroyed, but be sure to unbind it in time. Otherwise, the leak
2. For the same Service, first startService and then bindService. How can I stop it?
You only need to call stopService and unbindService at the same time. The order of the two is interchangeable.
3. What are the differences between different return values of the Service onStartCommond method?
Public @ StartResult int onStartCommand (Intent intent, @ StartArgFlags int flags, int startId ){
OnStart (intent, startId );
Return mStartCompatibility START_STICKY_COMPATIBILITY: START_STICKY;
}
There are two default values: START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT, and START_STICKY_COMPATIBILITY.
START_STICKY is used as the return value. After the program is killed due to an exception (the service is restarted, but the intent object is cleared), START_STICKY is used as the return value.
Value, applicable to media players (or similar services) that do not execute commands. It only runs indefinitely and waits for the arrival of work.
START_NOT_STICKY is used as the return value. If the program is killed due to an exception (the service is not rebuilt), this is the safest option.
START_REDELIVER_INTENT is used as the return value. After the program is killed abnormally (the service is restarted and the intent object is retained ),
The intent object data can be retained until the stopSelf method is called. This applies to services that should immediately restore ongoing work, such as downloading files.
START_STICKY_COMPATIBILITY is used as the return value. The compatible version of START_STICKY is not guaranteed to be restarted after the service is terminated. Program
After an exception is killed (the service is rebuilt but not restarted)
4. Which thread does the onCreate onStart onBind of the Service life cycle method run on?
Printed thread identifier and found to be running in the main thread. Therefore, executing time-consuming operations here may also cause ANR (Activity time-consuming operations more than 5 s,
Broadcast explorer has exceeded 10 s, and Service has exceeded 15 s), but IntentService is very secure. In other people's answers, internal use
HandlerThread. Check the source code and find that the source code is very short. Create HandlerThread in onCreate, obtain the logoff, and then pass the logoff
ServiceHandler, and then start to process the message. After processing, call stopSelf
The official documents are also clear.
* IntentService will receive the Intents, launch a worker thread, and stop the service
* Appropriate.
*
*
All requests are handled on a single worker thread -- they may take
* Long as necessary (and will not block the application's main loop),
* Only one request will be processed at a time.
5. What is the relationship between Service and Thread?
Services are generally run in the background (there are also front-end services), which makes people feel like Thread processing time-consuming tasks. In fact, improper use of services in the main Thread will also result in ANR,
All services have nothing to do with threads. The Service can run in the background without relying on the UI. The real-time Activity is destroyed and the program is closed, as long as the process is still running,
The Service can run. You can create sub-threads in the Service or use IntentService to process time-consuming tasks. The sub-threads created in the service are more
What are the benefits? Because other activities can also be associated with the Service, the Binder instance in the Service can be obtained and controlled freely, even if an Activity
Other activities can be controlled after finish, but unlike Thread, the Activity created by Thread cannot be controlled after destruction.