One, the code
1.xml
(1) Activity_main.xml
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9tools:context= "Com.service2.MainActivity" >Ten One<Button AAndroid:id= "@+id/bindbtn" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theAndroid:text= "bind operation complete"/> - -</RelativeLayout>
(2) Androidmanifest.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.service2"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten One<Application AAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" theAndroid:theme= "@style/apptheme" > -<Activity -Android:name= ". Mainactivity " -Android:label= "@string/app_name" > +<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> + A<category android:name= "Android.intent.category.LAUNCHER"/> at</intent-filter> -</activity> -<service android:name= ". Secondservice "></service> -</application> - -</manifest>
2.java
(1) Mainactivity.java
1 PackageCom.service2;2 3 Importandroid.app.Activity;4 ImportAndroid.content.ComponentName;5 Importandroid.content.Intent;6 Importandroid.content.ServiceConnection;7 ImportAndroid.os.Bundle;8 ImportAndroid.os.IBinder;9 ImportAndroid.view.View;Ten ImportAndroid.view.View.OnClickListener; One ImportAndroid.widget.Button; A - ImportCom.service2.SecondService.FirstBinder; - the Public classMainactivityextendsActivity { - - PrivateButton bindbtn =NULL; - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); ABINDBTN =(Button) Findviewbyid (R.ID.BINDBTN); atBindbtn.setonclicklistener (NewOnclicklistener () { - @Override - Public voidOnClick (View v) { -Intent Intent =NewIntent (); -Intent.setclass (mainactivity. This, Secondservice.class); - Bindservice (Intent, Conn, bind_auto_create); in } - }); to } + -Serviceconnection conn =Newserviceconnection () { the @Override * Public voidonservicedisconnected (componentname name) { $System.out.println ("onservicedisconnected>>>>>>");Panax Notoginseng } - @Override the Public voidonserviceconnected (componentname name, IBinder service) { +Firstbinder Binder =(firstbinder) service; ASystem.out.println ("onserviceconnected>>>>>" +binder.getdata ()); the } + }; -}
(2) Secondservice.java
1 PackageCom.service2;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.Binder;6 ImportAndroid.os.IBinder;7 8 Public classSecondserviceextendsService {9 Ten @Override One Publicibinder onbind (Intent Intent) { AIBinder Binder =NewFirstbinder (); - returnBinder; - } the - classFirstbinderextendsBinder { - PublicString GetData () { - return"Test Data"; + } - } +}
Android_mars Learning Note _s01 Original version _017_ binding service