The opening and binding of Android service, and the method of invoking service

Source: Internet
Author: User

Interface:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        >    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Open service"Android:onclick= "Start"            />    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Close Service"Android:onclick= "Close"            />    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Bind service"Android:onclick= "Bind"            />    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Unbind Service"Android:onclick= "Unbind"            />    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "method function to invoke service ()"Android:onclick= "CallFunction"            /></LinearLayout>

Activity

 Packagecom.example.serviceTest;Importandroid.app.Activity;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.view.View; Public classMyActivityextendsActivity {PrivateIService Mybinder; PrivateServiceconnection myconn =Newmyconn (); @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); }     Public voidStart (view view) {StartService (NewIntent ( This, Testservice.class)); }    /*** Multiple calls to stop service, no problem (cannot be unbound multiple times)*/     Public voidClose (view view) {StopService (NewIntent ( This, Testservice.class)); }     Public voidbind (view view) {Intent Intent=NewIntent ( This, Testservice.class);    Bindservice (Intent, myconn, bind_auto_create); }    /*** Multiple Unbind service will report an error*/     Public voidUnbind (view view) {Unbindservice (myconn); }     Public voidcallfunction (view view) {if(Mybinder! =NULL) {mybinder.callfunction (); }    }    //when binding, some of the callback methods    classMyConnImplementsserviceconnection {@Override Public voidonserviceconnected (componentname name, IBinder service) {System.out.println ("Get to Binder"); Mybinder=(iservice) service; } @Override Public voidonservicedisconnected (componentname name) {System.out.println ("Com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected"); }    }}


Service

 Packagecom.example.serviceTest;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.widget.Toast;/*** Created by Heyiyong on 14-5-16.*/ Public classTestserviceextendsService { Publicibinder onbind (Intent Intent) {System.out.println ("The service is tied up!" "); return NewMybinder (); }    //Intermediary (agent of service)    Private classMybinderextendsBinderImplementsiservice{ Public voidcallfunction () {function (); }} @Override Public voidonCreate () {System.out.println ("Com.example.serviceTest.TestService.onCreate"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {System.out.println ("Com.example.serviceTest.TestService.onStartCommand"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {System.out.println ("Com.example.serviceTest.TestService.onDestroy"); } @Override Public BooleanOnunbind (Intent Intent) {System.out.println ("Com.example.serviceTest.TestService.onUnbind"); return Super. Onunbind (Intent); }     Public voidfunction () {Toast.maketext (), Getapplicationcontext (),The function () method is called! ", 1). Show (); System.out.println ("Com.example.serviceTest.TestService.function"); }}

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.