Android Service and Activity communication

Source: Internet
Author: User

We need to start the service in the following ways:

</pre><pre name= "code" class= "java" ><span style= "White-space:pre" ></span>intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Myservice.class); <strong>bindservice (intent,conn,bind_auto_ CREATE);</strong>


Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {}@ overridepublic void onserviceconnected (componentname name, IBinder service) {Mservice = ((myservice.mybinder) service). GetService ();  }};


Then the service executes: OnCreate and Onbind


And then


We're going to perform some tasks:

ImageView item_image = (ImageView) Findviewbyid (r.id.item_image); Item_image.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {<strong>mservice.sethandler (mhandler);   Mservice.startcheckifupgrade (); </strong>});p rivate Handler mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case GET_UPGRADE_INFORMATION_COMPLETE:System.out.println ("======= Notify need Upgrade "); Alertdialog dialog = Getcustomdialog ();d ialog.show ();d efault:break;}};

The code for the service is:

public class MyService extends Service {/** thread to execute background task*/private thread mthread = null;/** Flag indi Cates If the service has been stopped*/private boolean mstopflag = false;private static final int get_upgrade_information_ Complete = 202;/** Handler to get notify from thread*/private Handler Mhandler = null;public void SetHandler (Handler handl ER) {this.mhandler = handler;} @Overridepublic boolean onunbind (Intent Intent) {System.out.println ("====service onunbind"); return Super.onunbind ( Intent);} public class Mybinder extends Binder {/** * Gets an instance of the current service * * @return */public myservice getService () {return myservice. This;}}   public void Startcheckifupgrade () {if (Mthread = = null) {Mthread = new upgradethread (); Mthread.start ();}} @Overridepublic ibinder onbind (Intent arg0) {System.out.println ("====service onbind"); return new Mybinder ();} /** * @see android.app.service#oncreate () */@Overridepublic void OnCreate () {super.oncreate (); System.out.println ("====service onCreate");}private class Upgradethread extends thread{/** * @see java.lang.thread#run () */@Overridepublic void Run () {super.run (); /write to the server to see if you need to upgrade the logical try {thread.sleep),} catch (Interruptedexception e) {e.printstacktrace ();} boolean ret = true;if (ret &&!mstopflag) {message msg = new Message (); msg.what = Get_upgrade_information_complete; Mhandler.sendmessage (msg);}}} /* (non-javadoc) * @see Android.app.service#ondestroy () */@Overridepublic void OnDestroy () {System.out.println ("= = OnDestroy "); Mstopflag = true;try {if (null! = Mthread && mthread.isalive ()) {try{mthread.stop ();} catch (Exception e) {}}mthread = null;} catch (Exception e) {e.printstacktrace ();} Super.ondestroy (); System.GC ();}}

When the activity is destroyed, the service executes:

Onunbind and OnDestroy

This indicates that the service's life cycle and activity are consistent

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.