Tag: Indicates close create RRI art nload parameter registration connected
Create a service and bind to the activityas one of the four main components of Android services, no exception to be registered in the Manifast
- The new service class inherits from the service and overrides the Onbind () method for binding with the active
Public classMyseviceextendsService {//Create a Downloadbinder object Mbinder PrivateDownloadbinder Mbinder =NewDownloadbinder (); //Create a Downloadbinder class that implements the method that the service needs to wait for activity instructions to execute classDownloadbinderextendsBinder {
//Must be a method of Pubilc modification Public intgetprocess () {return0; }} @Nullable @Override//returns the Downloadbinder object Mbinder Publicibinder onbind (Intent Intent) {returnMbinder; } @Override//execute when the service is created Public voidonCreate () {Toast.maketext ( This, "Service Creation succeeded", Toast.length_long). Show (); Super. OnCreate (); } @Override//Execute when service is started Public intOnstartcommand (Intent Intent,intFlagsintStartid) {Toast.maketext ( This, "Service started successfully", Toast.length_long). Show (); return Super. Onstartcommand (Intent, flags, Startid); } @Override//Execute when service is closed Public voidOnDestroy () {Toast.maketext ( This, "Service shutdown succeeded", Toast.length_long). Show (); Super. OnDestroy (); }}
- Find the Mbinder object passed in the activity.
PrivateMysevice.downloadbinder Mdownloadbinder; //Create an anonymous inner class serviceconnection (), override Method Onserviceconnected (), and onservicedisconnected () are called at bind and unbind intervals, respectively PrivateServiceconnection mconnection =Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {//change down to find the Mdownloadbinder object and invoke the public method in the objectMdownloadbinder =(mysevice.downloadbinder) service; Mdownloadbinder.getprocess (); } @Override Public voidonservicedisconnected (componentname name) {}};
- Binding services and activities
New Intent (threaddemoactivity. this, Mysevice. class ); Bindservice (thread_bind_service,mconnection,bind_auto_create); the bindservice () method receives three parameters, the first parameter is the Intent object that was just built, and the second parameter is an instance of the Serviceconnection created earlier, * The third parameter is a flag bit, where the incoming bind_auto_create indicates that the service is created automatically after the activity and the service are bound */
Unbindservice (mconnection);
Android Four Components service