Recently saw the doctor's "Android Art development exploration" feel very fruitful. So also want to record a blog, because blogging is really the accumulation and summary of the process. All right, no more talking nonsense.
Service is divided into 2 kinds, one is the ordinary service, is directly inherit the service class, relatively simple. The main thing to note is that this class runs on the main thread and cannot do time-consuming work. If you need to do time-consuming work, I need to open another thread.
Based on the official documentation of the service's basic method of interpretation:
Onstartcommand (): When other components call the StartService () method, this method is called once and runs in the background. If you implement a method, you need to call stopself or StopService after this method is completed (if you just want to get the binding you don't have to implement this method). This is also the basic service to work on the method, the function you want to implement here. One thing to note is that this method is not invoked when you open the service through Bindservice
The Onstartcommand callback method also has a return value, which gives the system a total of 3:
1, Start_not_sticky the value of the document says that the system will not easily kill the service before you complete the work. is a relatively safe value. The system will not re-create this service
2, Start_sticky When we return this value, we re-create the service, but we will not pass the intent object again. This is more suitable for multimedia playback
Onbind (): When other components want to bind to the service (for example, to implement a remote protocol call RPC), in this method implementation you must provide the IBinder interface to communicate with the client. Methods that must be implemented to implement cross-process communication
onCreate (): When the service is first created, the system callback, you can do some initialization work here
OnDestroy (): When the service is destroyed, the system callback, do some free memory operation
There is also a function that can also implement the service, that is Intentservice, this and the above is the difference is that we have to operate in the onhandleintent. And this callback is time-consuming to work with.
The following is the start service, start the service mainly two ways, the first is StartService, when we start a implementation of the Onstartcommand service, OnCreate after the call, and start again will not call the method.
This article is from the "first entry Hermitage" blog, please make sure to keep this source http://wangjinxue.blog.51cto.com/3819301/1714734
Android Aidl and normal service