Android Service Control Usage Example Analysis _android

Source: Internet
Author: User
Tags stub

The examples in this article describe the usage of the Android service control. Share to everyone for your reference, specific as follows:

1. Service is a component of an application
2, the service does not have the graphical interface
3, to deal with time-consuming functions (download, play MP3)
4, update ContentProvider, intent and system start-up

Servcie is not a separate process, not a thread

Defining a service is simpler, as long as it inherits the service class and realizes its lifecycle. A defined service must be passed <service> declared in the Androidmanifest.xml file to use

<service android:name= "MyService" >
  <intent-filter>
    <action android:name= "Hb.com.MYSERVICE" "/>
  </intent-filter>
</service>

Note: MyService must inherit the service class, and the name and class name are consistent

The name of the action is customized and can be captured as long as it is bindservice or manipulated intent

 public class MyService extends Service {public class Mybinder extends binder{pub
    LIC MyService Getmyservice () {return myservice.this;
  } public void Test () {System.out.println ("test"); @Override public IBinder onbind (Intent Intent) {//TODO auto-generated Method Stub System.out.println ("Onbi
    nd ");
  return new Mybinder ();
    @Override public void OnCreate () {//TODO auto-generated the method stub System.out.println ("OnCreate");
  Super.oncreate ();
    @Override public boolean onunbind (Intent Intent) {System.out.println ("I am Unbind");
  return Super.onunbind (Intent); }
}

public class Mainactivity extends activity {private static final String MyService = "Hb.com.MYSERVICE";
  Private Boolean flag = false; The Bindservice () method requires the Serviceconnection interface as a parameter, so the variable is defined to implement the two methods inside serviceconnection conn = new Serviceconnection ( {@Override public void onservicedisconnected (componentname name) {System.out.println ("Onservicedisconnec
    Ted "); @Override public void onserviceconnected (componentname name, IBinder service) {System.out.println ("Onserv
      Iceconnected ");
      Mybinder Mybinder = (mybinder) service;
      MyService MyService = Mybinder.getmyservice ();
    Myservice.test ();
  }
  };
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main); Findviewbyid (R.id.start). Setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View V
        ) {Intent Intent = new Intent (); Intent.setaction (MyserVICE); Call the MyService OnCreate () method first, and then call the Onbind () method, and the Onserviceconnected () method is invoked, and therefore the value returned by IBinder is the object returned through the Onbind () method. The Getmyservice () method is defined to get the MyService object, in which the user adds some of the methods they need to do some logical processing bindservice (INTENT, Conn, service.bind_auto_cre
        ATE);
      Flag = true;
    }
    });  Findviewbyid (r.id.stop). Setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View v) {if (flag) {//Unbind event, the MyService Onunbind () method is invoked, but the onservicedisconnected () method is not invoked for unexplained reasons unbinds
          Ervice (conn);
        Flag = false;
  }
      }
    });

 }
}

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.