1.1 Activities and services for communication
In the previous section, we learned how to start and stop a service. I do not know you have not found, although the service is initiated in the activity, but after the start of the service, activities and services are basically not okay, indeed, we call the StartService () method in the activity to start myservice this service, Then MyService's OnCreate () and Onstartcommand () methods are executed. After the service has been in operation, but the specific operation of what logic, activities will not be able to control. This is similar to the event Notification Service: ' You can start up! Then the service goes to work on its own, but the activity doesn't know what the service is doing, and how it's done.
So is there any way to make the relationship between activities and services more intimate? For example, in the event of directing the service to do, the service to do. Of course, this requires the use of the Onbind () method that we have just overlooked.
For example, we want to provide a download function in Servicetest, and then we can decide when to start the download and check the download progress at any time. The idea to do this is to create a dedicated binder object to manage the download, modifying the code in Servicetest as follows:
public class Servicetest extends service{
Private Downloadbinder mbinder=new Downloadbinder ();
Class Downloadbinder extends binder{
public void Startdownload () {
LOG.D ("MyService", "Startdownload executed");
}
public int getprogress () {
LOG.D ("MyService", "getprogress executed");
return 0;
}
}
@Override
Public IBinder Onbind (Intent Intent) {
return mbinder;
}
}
As you can see, we've created a new Downloadbinder class here, and let it inherit from Binder, and then inside it provides a way to download and view the progress of the download. Of course, this is only two simulation methods, and does not realize the real function. Next, an instance of Downloadbinder is created in Servicetest, and then returned in the Onbind () method, so that the work in Servicetest is complete.
1.2 Life cycle of service
Methods such as OnCreate (), Onstartcommand (), Onbind (), and OnDestroy (), which we used earlier, are callbacks in the life cycle of the service. Once the context's StartService () method is called at any point in the project, the corresponding service will start up and callback the Onstartcommand () method. If this service has not been created before. The OnCreate () method is executed before the Onstartcommand () method. The service starts up and stays running, and Onstartcommand executes once, but there is actually only one instance per service. So no matter how many times you call the StartService () method, simply call the StopService or Stopself () method and the service will stop.
Alternatively, you can call the context's Bindservice () to get a persistent connection to a service, and then callback the Onbind () method in the service. Similarly, if the service has not been created before, the OnCreate () method will be executed prior to the Onbind () method. The caller can then obtain an instance of the IBinder object returned in the Onbind () method so that it can communicate freely with the service. As long as the connection between the caller and the service is not broken, the service remains in a running state. After the StartService method is called. The StopService () method is called again, and the OnDestroy () method in the service executes, indicating that the service has been destroyed. Similarly, when the Bindservice () method is called and the Unbindservice () method is called, the OnDestroy () method executes, both of which are well understood. However, it is important to note that it is entirely possible for a service to call both the StartService () method and the Bindservice method, in which case the service can be destroyed. Depending on the mechanism of the Android system, a service can be destroyed only after it has been started or bound, and it has been in a state of operation and must not satisfy both of these conditions. Therefore, the StopService () and Unbindservice () method OnDestroy () methods are called to execute at the same time in this case.
Android Service (ii)