1.The service, like all other components, runs in the main thread, so it cannot be used for time-consuming operations.2.The type of process in Android Foreground process foreground process visible processes visible, can be seen but not interactive. Service process Background processes the process daemon empty process (when the program exits, it is not destroyed, but it becomes an empty process)3.When the recycling service, the system is not easy to recycle, if you need to reclaim the three processes, then the system memory is sufficient, the restart process will be given, but the service process if the user manually shut down the service, then the service will not restart.4.Why use a service instead of a thread Android application just started will open a process for this program to be used. Android an application to turn off all the interface, the process is not destroyed at this time, in the empty process state, thread running in the empty process, it is easy to be destroyed. The service is not easily destroyed, and if it is destroyed in an illegal state, the system restarts when the memory is sufficient.5.How to use service to create service, define class inheritance service, define <service> open service in Androidmanifest.xml, call StartService method in other components starts Ervice (Intent), (which can be called directly in the activity), executes the Onstartcommand (life cycle method), and if the service is not found, the execution OnCreate is created first. OnCreate will only execute once, once the service is created, the OnCreate method will not be executed, and the service will only execute Onstartcommand later. Stop the service, call the StopService method, execute the Ondestory () (Life cycle method) service is also running in the main thread, the time-consuming operation needs to be placed in the child thread, or the ANR Setforegound will appear (true(called directly in the service), in order to avoid the service being killed, we can specify this service as the foreground process. The service is also a foreground process when it executes the life cycle approach. The same is true for broadcast receivers, which is also a foreground process when the life cycle method OnReceive is executed.6. /** * Called when binding the service * / @Override PublicIBinderOnbind(Intent Intent) {return NULL; } Telephonymanager Telephonymanager = (telephonymanager) getsystemservice (Telephony_service); Telephonymanager.listen (NewMyphonelistener (), phonestatelistener.listen_call_state); Status: Telephonemanager.call_state_idle: The phone is idle, call_state_ringing, the phone is in a ringing state. Call_state_offbook: Unlocked, the phone is in the answer state. Start Service:intent Service in the Mainactivity class =NewIntent (); Service.setclass ( This, Myservice.class); StartService (service); <uses-permission android:name="Android.permission.READ_PHONE_STATE"/> <uses-permission android:name="Android.permission.RECORD_AUDIO"/>7.The service life cycle can have two different paths. There are two ways to open a service, one is direct onstart and the other is a binding service. Started service (standard open mode) other component calls StartService () are created. This service can run indefinitely and must call the Stopself () method or other component to call the StopService () method to stop it bound service (binding mode) other components (a customer) call Bindservice () to create the , customers can communicate via a IBinder interface and service. Customers can use the Unbindservice () method to close this connection. A service can be bound to multiple clients at the same time, and when multiple customers are unbound, the service binding services method is destroyed: Bindservice (Intent,serviceconnection,intflag); Call this method in other components , after the invocation of the service internal life cycle method Onbind method execution, once the binding succeeds, Onbind only executes once, binds again, Onbind will not execute. The second parameter: serviceconnection: A Connection object, a method that can invoke a service after the service is connected to a custom serviceconnection to receive IBinder. Instantiate the Serviceconnection object, overriding the internal method: onserviceconnected (ComponentName Name,ibinderbinder): The method that is executed when the binding obtains the connection successfully. Parameters: IBinder object: The Receiving service internal Onbind method, the returned object (through which the object can indirectly access the method inside the service) the third parameter: Bind_auto_create, if the binding of this service is not created, then the service is created automatically, Call the life cycle Method OnCreate () method. Unbindservice () Unbind the service, internally invokes the service's life cycle Method Onunbind (), and then calls the Ondestory () destroy service. The service can only be unbound once, and if the Unbindservice method is called multiple times, an error occurs8.The difference between startup and binding starts the service directly through start (): Once the service is turned on, it has no relationship with the caller, and the caller's activity does not affect the running of the background service even if it exits. Through the Bindservice () binding service: A service that is opened by way of binding, the service does not seek the same life as the caller but dies. Note: If the activity of a program is bound to a service, the activity exits with an exception, saying that the service has not been released. Then we can rewrite the activity's Ondestory method, call Unbindservice () inside the method, and go to show the unbind with the service. The second difference: Start a method that starts the service directly, and the caller cannot invoke a method inside the service. The method by which the binding service starts the service, the caller can invoke the internal method of the service to take advantage of the Serviceconnection interface to get the IBinder object returned by the service Onbind, which implements a custom interface that defines the methods within the service.
Service of 2016-05-10android four components