Android Four Component Learning Service four

Source: Internet
Author: User
<span id="Label3"></p><p><p>This section learns the life cycle of the Service. Since activity has a life cycle, the empathy service has its own life cycle.</p></p><p><p>As with learning activity, we first post the definition of the Google website to the service, as well as the life cycle diagram</p></p><p><p></p></p><p><p>As you can see, the way to the left is what we said in the second lesson. use StartServer to start the service Process. The right way down is the third section of the process that starts the service with Bindservice.</p></p><p><p>In the previous two sections, you saw how to start a service and destroy the Service. Since this is a mix-and-match era, it is also necessary for these two services to be mixed up and used.</p></p><p><p>Let's take a look at Myactivity's code:</p></p><p><p></p></p><pre name="code" class="java">public class MyActivity extends Activity {private button btn_start;private button btn_end;private button btn_bind;private Button btn_unbind;private myserviceconnection sconnection, @Overrideprotected void onCreate (Bundle savedinstancestate ) {//TODO auto-generated method stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_myservice) ; Btn_start = (button) Findviewbyid (r.id.button1); btn_end = (button) Findviewbyid (r.id.button2); btn_bind = (button) Findviewbyid (r.id.button3); btn_unbind = (Button) Findviewbyid (r.id.button4); btn_start.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//start Service Intent Intent = new Intent (myactivity.this, Myservice. class); StartService (intent); LOG.I ("myactivity", "create Service button is pressed!");}); Btn_end.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View arg0) {//destroy Service Intent intent = new Int ENT (myactivity.this, myservice.class); stopservice (intent); LOG.I ("myactivity", "destroy Service button is pressed!");}); Btn_bind.setonclicklisTener (new onclicklistener () {@Overridepublic void OnClick (View arg0) {//start Service Intent Intent = new Intent (myactivity.this, M yservice.class); LOG.I ("myactivity", "bind Service button is pressed!"); Bindservice (intent, sconnection, service.bind_auto_create);}); Btn_unbind.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View arg0) {//start Service Intent Intent = new Intent (myactivity.this, myservice.class); LOG.I ("myactivity", "unbind Service button is pressed!"); Unbindservice (sconnection);}});} public class Myserviceconnection implements Serviceconnection{@Override//call public void onserviceconnected When the connection service is successful ( ComponentName arg0, ibinder arg1) {//TODO auto-generated method stublog.i ("myactivity", "onserviceconnected");} Call public void onservicedisconnected (componentname arg0) {//TODO auto-generated @Override//when The process where the server is located is terminated due to an unexpected termination or other reason Method STUBLOG.I ("myactivity", "onservicedisconnected");}}}</pre><br>MyService code: (and The MyService Code of the previous section is Kind)<br><pre name="code" class="java">public class MyService extends Service {private MyThread thread;private boolean stopflag = false; @Override//methods that must be implemented public IBinder onbind (Intent arg0) {//TODO auto-generated method stub//service after running, start thread if (!stopflag) {thread.start (); Thread.setflag (true);} LOG.I ("myservice", "onbind------------"); return null;} @Override//is created when calling public void OnCreate () {//TODO auto-generated method stub//when The service is created, instantiate mythreadthread = new MyThread (); LOG.I ("myservice", "onCreate------------"); super.oncreate ();} @Override @deprecated//onstart method is now replaced by onstartcommd, in fact Onstartcommand also called Onstartpublic void OnStart (Intent Intent, int Startid) {//TODO auto-generated method Stubsuper.onstart (intent, startid); LOG.I ("myservice", "onStart------------");} @Override//start call public int onstartcommand (Intent Intent, int flags, int Startid) {//TODO auto-generated method stublog.i ("myservice", "onstartcommand------------");//after Service runs, start thread if (!stopflag) {thread.start (); thread.setflag (true);} return Super.onstartcommand (intent, flags, startid);}@Overridepublic void OnDestroy () {//TODO auto-generated method stub//when The service is destroyed, stop the thread task Thread.setflag (false); LOG.I ("myservice", "ondestroy------------"); Super.ondestroy ();} @Overridepublic boolean onunbind (Intent Intent) {//TODO auto-generated method stublog.i ("myservice", " onunbind------------"); return Super.onunbind (intent);} Class MyThread extends Thread{//set flagpublic void Setflag (Boolean flag) {stopflag = flag;} @Overridepublic void Run () {super.run (); while (stopflag) {//sets the time output mode SimpleDateFormat format = new SimpleDateFormat (" YYYY-MM-DD HH:mm:ss "); String time = Format.format (new Date ()),//display log.i ("myservice"), try {//delay One second thread.sleep (n);} catch ( Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}</pre><br><br><p><p></p></p><p><p>Now that you have to learn the mix and match, there are several ways to mix and match:</p></p><p><p>1: start the service with StartServer and then use Bindservice to bind the service</p></p><p><p></p></p><p><p>You can see that the StartServer starts the service first, and the service is bound with Bindservice</p></p><p><p>Then we stop the service with Stopservice.</p></p><p><p></p></p><p><p>You can see that the service cannot be Stopped.</p></p><p><p>well, If we use Unbindservice first to unbind the service,</p></p><p><p></p></p><p><p>You can see that the service can be stopped by using the Unbind Service.</p></p><p><p>2: bind the service with Bindservice and then go to start the service with StartServer<br></p></p><p><p></p></p><p><p>Can see still press stop service is not possible, you must first unbind the service</p></p><p><p><br></p></p><p><p>That concludes: when mixing calls, whether it is to start the service or the binding service, and finally need to unbind the service, but not to bind the service, service is not able to stop.</p></p><p><p><br></p></p><p><p>Then look at a phenomenon, if the service is running, I will current activity directly press the back key to exit, what will be the effect?</p></p><p><p>first, Add the following method to the MyActivity</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">@Overridepublic boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method stubif (keycode = = KEYEVENT.K Eycode_back) {log.i ("myactivity", "keycode_back is pressed!");}    return Super.onkeydown (keycode, event);}        @Override protected void OnDestroy () {log.i ("myactivity", "ondestroy");    Super.ondestroy (); }</pre></pre><br><p><p></p></p><p><p>1: start the service with StartService and then exit activity</p></p><p><p><br></p></p><p><p>You can see that the activity is destroyed when I press the back button, but our service is still running. This can indicate that the StartServer start-up service is not related to activity</p></p><p><p>If the service is stopped, you must set->app-> to stop the service in the running</p></p><p><p><br></p></p><p><p>2: start the service with Bindservice and then exit</p></p><p><p></p></p><p><p>You can see starting the service with bindservice, and when activity exits, the service Stops. Description Bindservice The services that are started are related to Actiivty.</p></p><p><p><br></p></p><p><p><br></p></p><p><p>well, after reading this, we're back on our mash-up service.</p></p><p><p>if, Let us start the service in a mash-up way, without unbinding, We are quitting the activity directly.</p></p><p><p></p></p><p><p>Because the Bindservice start-up service is associated with activity, activity destruction is also untied. But we have a useful startserver to start the service, so the service is still running. There is an exception about the direct exit. We can call Onunbindservice in the Destory method that the activity Exits.</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">@Overrideprotected void OnDestroy () {log.i ("myactivity", "ondestroy"); unbindservice (sconnection); Super.ondestroy () ;}</pre></pre>Then exit directly and there will be no abnormal printing.<p><p></p></p><p><p><br></p></p><p><p><br></p></p><p><p>okay, Here's The life cycle of Serive.<br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Four Component Learning Service four</p></p></span>

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.