I have a jug of wine. Android Learning Service (1)--->binderservice way

Source: Internet
Author: User

This article discusses only the extension binder class

Create a Binder.xml

<?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:id= "@+id/btnstartbinderservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start binderservice"        />    <ButtonAndroid:id= "@+id/btnstopbinderservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Stop binderservice"        /></LinearLayout>

Create a Binderservice.jvaa class that inherits the service

 PackageCom.szy.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classBinderserviceextendsservice{Private Static FinalString TAG = "Binderservice"; PrivateMybinder Binder =NewMybinder ();  Public classMybinderextendsBinder { PublicBinderservice GetService () {returnBinderservice. This; }} @Override Publicibinder onbind (Intent Intent) {returnBinder; }         Public voidMyMethod () {log.i (TAG,"MyMethod ()"); }}

Create a new class Binderactivity.java inherit activity

 PackageCom.szy.service;ImportCom.szy.service.BinderService.MyBinder;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classBinderactivityextendsactivity{PrivateButton Btnstartbinderservice; PrivateButton Btnstopbinderservice; PrivateBoolean isconnected =false; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.binder); Btnstartbinderservice=(Button) Findviewbyid (R.id.btnstartbinderservice); Btnstopbinderservice=(Button) Findviewbyid (R.id.btnstopbinderservice);        Btnstartbinderservice.setonclicklistener (listener);    Btnstopbinderservice.setonclicklistener (listener); }        PrivateOnclicklistener listener=NewOnclicklistener () { Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btnstartbinderservice:bindservice ();  Break;  CaseR.id.btnstopbinderservice:unbind ();  Break; default:                 Break;        }        }    }; Private voidUnBind () {if(isconnected) {unbindservice (conn); }    }    Private voidBindservice () {Intent Intent=NewIntent (binderactivity. This, Binderservice.class);    Bindservice (Intent, Conn, context.bind_auto_create); }        PrivateServiceconnection conn=Newserviceconnection () { Public voidonservicedisconnected (componentname name) {isconnected=false; }                     Public voidonserviceconnected (componentname name, IBinder binder) {Mybinder Mybinder=(Mybinder) binder; Binderservice Service=Mybinder.getservice (); Service.            MyMethod (); IsConnected=true; }    };}

Modify the next Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.szy.service"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "8" />    <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name">        </Activity>                <ActivityAndroid:name=". Binderactivity "Android:label= "@string/app_name">        </Activity>                <ActivityAndroid:name=". Intentactivity "Android:label= "@string/app_name">             <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>                <ServiceAndroid:name=". Exampleservice " />        <ServiceAndroid:name=". Binderservice " />        <ServiceAndroid:name=". MyService "/>        <ServiceAndroid:name=". Exampleintentservice "/>    </Application></Manifest>

I have a jug of wine. Android Learning Service (1)--->binderservice way

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.