Android Service Details (ii)

Source: Internet
Author: User

There are four important functions in the service:

Public IBinder Onbind (Intent arg0);//must be implemented, return interface to service public void onCreate (); Call pub on//service creation LIC void OnStart (Intent intent,int Startid);//Call public void StartService () through OnDestroy ();//Destroy Stopserv Ice () call


The service is started by the startactivity () function, and when the first call is called, OnCreate () and onstart are called respectively ();

Then only the OnStart () is called;

End Service through function StopService (), call OnDestroy ();

Call Bindservice (): Call OnCreate () and Onbind () when the service is not created; only call Onbind () when it is created;

Use function Bindservice () and function Unbindservice () to bind and unbind

The service call Bindservice () that is already bound is not valid, that is, multiple calls to Bindservice () and one Call to Bindservice (). Unbindservice () can only be used once, that is, for a bound service, only one unbindservice () can be called, and multiple calls produce an error


The function prototype is:

Bindservice (intent,serviceconnection,bind_auto_create);

Serviceconnection is a service connection class that must implement the following two functions:

public void onserviceconnected (ComponentName arg0, IBinder arg1)//Call public void onservicedisconnected When the connection is successful ( ComponentName arg0)//Call when connection fails

Examples are as follows:

Private serviceconnection conn=new serviceconnection ()  {     @Override     public void onserviceconnected (componentname arg0, ibinder  ARG1)  {        // TODO Auto-generated method  Stub        toast.maketext (mainactivity.this,  "Success",  Toast.length_long). Show (),         LOG.I ("SERVICE", "success");     }     @Override     public void  Onservicedisconnected (componentname arg0)  {        //  Todo auto-generated method stub        toast.maketext ( mainactivity.this,  "Errer",  toast.length_long);         log.i ("SERVICE", "Errer");     } 



Service instance:

Mainactivity.java:

Private serviceconnection conn=new serviceconnection ()  {     @Override     public void onserviceconnected (componentname arg0, ibinder  ARG1)  {        // TODO Auto-generated method  Stub        toast.maketext (mainactivity.this,  "Success",  Toast.length_long). Show (),         LOG.I ("SERVICE", "success");     }     @Override     public void  Onservicedisconnected (componentname arg0)  {        //  Todo auto-generated method stub        toast.maketext ( mainactivity.this,  "Errer",  toast.length_long);         log.i ("SERVICE", "Errer");     }}; 
Protected void oncreate (bundle savedinstancestate)  {    super.oncreate (savedinstancestate);     setcontentview (R.layout.activity_main);     button button1= (Button) This.findviewbyid (R.ID.BTN1);     button button2= (Button) This.findviewbyid (R.ID.BTN3);     button button3= (Button) This.findviewbyid (R.ID.BTN4);     button button4= (Button) This.findviewbyid (R.ID.BTN5);     Button1.setonclicklistener (New onclicklistener ()  {        @ Override        public void onclick (View arg0)  {             // TODO Auto-generated  Method stub            startservice (new  Intent (mainactivity.tHis,newservice.class));         }    });     button2.setonclicklistener (New onclicklistener ()  {          @Override         public void onclick (View  ARG0)  {            // TODO  auto-generated method stub             StopService (New intent (mainactivity.this,newservice.class));         }    });     button3.setonclicklistener (New OnClickListener ( )  {         @Override          public void onclick (view arg0)  {             // todo auto-generated method stub             bindservice (New intent (Mainactivity.this,newservice.class), conn,bind_auto_create);         }    });     Button4.setonclicklistener (New onclicklistener ()  {        @ Override        public void onclick (View arg0)  {             // TODO Auto-generated  Method stub            unbindservice (conn);         }    });}

Newservice.java:

public class newservice extends service {     @Override      public ibinder onbind (intent arg0)  {         // TODO Auto-generated method stub         toast.maketext (newservice.this,  "Onbind",  toast.length_long). Show ();         LOG.I ("SERVICE", "Onbind");        return  Null;    }    public void oncreate ()  {         super.oncreate ();         LOG.I (" SERVICE "," oncreat ");         toast.maketext (newservice.this, " Oncreat ",  toast.length_long). Show ();     }    public void  onstart (intent intent,iNt startid)  {        log.i ("SERVICE", "OnStart");         toast.maketext (newservice.this,  "OnStart",  toast.length_long). Show ();     }    public void ondestroy ()  {         LOG.I ("SERVICE", "Ondestory");         toast.maketext (newservice.this,  "Ondestory",  toast.length_long). Show ();     }}

Activity.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"      Android:layout_width= "Fill_parent"     android:layout_height= "Fill_parent"      android:orientation= "vertical"  >    <button android:id= "@+id/ Btn1 "         android:layout_width=" Wrap_content "         android:layout_height= "Wrap_content"          android:text= "Start"/>    <button android:id= "@+id/btn3"           android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:text= "Stop"/>    <button android:id= "@+id/btn4"           android:Layout_width= "Wrap_content"         android:layout_height= "wrap_content "        android:text=" bind "/>    <button  android:id= "@+id/btn5"          android:layout_width= "Wrap_ Content "        android:layout_height=" Wrap_content "         android:text= "Unbind"/>    </linearlayout>

Androidmanifest.xml Increase:

<service android:name= "Com.example.new1.NewService"/>




This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1616665

Android Service Details (ii)

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.