How Android uses the service

Source: Internet
Author: User

Package Com.example.yabushan.hello3;import Android.app.service;import Android.content.intent;import Android.os.binder;import Android.os.ibinder;public class MyService extends Service {private Boolean service_state=true ;//Is True when the thread inside the Onstartcommand is executed
Public MyService () {} @Override public ibinder onbind (Intent Intent) {return new Binder (); } @Override public int onstartcommand (Intent Intent, int flags, int startid) {//System.out.println ("startcom Mand "); New Thread () {@Override public void run () {super.run (); while (service_state) {System.out.println ("service is running ... "); try {sleep (2000); } catch (Interruptedexception e) {e.printstacktrace (); }}}}.start (); Return Super.onstartcommand (Intent, flags, Startid); } @Override public void OnCreate () {super.oncreate (); SYSTEM.OUT.PRINTLN ("service create"); } @Override public void OnDestroy () {Super.ondestroy (); SYSTEM.OUT.PRINTLN ("service Destroy"); Service_state=false; }}

Package Com.example.yabushan.hello3;import Android.app.activity;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.view.view;public class Mainactivity extends Activity    Implements View.onclicklistener, serviceconnection {Intent Intent;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.my_layout);                Intent=new Intent (Mainactivity.this,myservice.class);                Findviewbyid (R.id.startservice). Setonclicklistener (this);                Findviewbyid (R.id.stopservice). Setonclicklistener (this);                Findviewbyid (R.id.bindservice). Setonclicklistener (this);        Findviewbyid (R.id.unbindservice). Setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {case R.Id.startService:startService (Intent);            Case R.id.stopservice:stopservice (Intent);            Case R.id.bindservice:bindservice (Intent,this, context.bind_auto_create);        Case R.id.unbindservice:unbindservice (this);    }} @Override protected void OnStart () {Super.onstart ();    } @Override protected void Onresume () {super.onresume ();    } @Override protected void OnPause () {super.onpause ();    } @Override protected void OnStop () {super.onstop ();    } @Override protected void Onrestart () {Super.onrestart ();    } @Override protected void OnDestroy () {Super.ondestroy (); } @Override public void onserviceconnected (componentname name, IBinder service) {System.out.println ("service binding succeeded"    ); } @Override public void onservicedisconnected (componentname name) {System.out.println("Service unbind successfully"); }}

Output from service startup:

11-21 05:32:23.028 3906-3906/? I/system.out:service Create
11-21 05:32:23.028 3906-3906/? I/system.out:startcommand
11-21 05:32:23.045 3906-3953/? I/system.out: Service is running ...
11-21 05:32:25.059 3906-3953/? I/system.out: Service is running ...
11-21 05:32:27.070 3906-3953/? I/system.out: Service is running ...

Service stopped:

11-21 05:32:37.314 3906-3906/? I/system.out:service Destroy

Service bindings:

11-21 05:33:07.262 3906-3906/? I/system.out:service Create
11-21 05:33:07.277 3906-3906/? I/system.out: Service binding succeeded

Service Unbind:

11-21 05:33:19.618 3906-3906/? I/system.out:service Destroy

How Android uses the service

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.