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