ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE
一、流程
1.编写service,重写onBind(Intent intent),返回自定义的Binder
2.自写义Binder,提供一个可访问的方法,以传递数据
3.点击界面按钮会开启service,过程为,通过Intent的setClass指定要开启的service,再通过bindService(intent, conn, BIND_AUTO_CREATE);开启service,其中参数conn是ServiceConnection,需要实现它的onServiceConnected(ComponentName name, IBinder service),service开启成功后,会回调这个函数,会把binder传给参数IBinder,从而实现数据传递,这就是所谓的给service提供了client-server模式
二、代码
1.xml
(1)activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.service2.MainActivity" > 10 11 <Button 12 android:id="@+id/bindBtn" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="完成绑定操作" /> 16 17 </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" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <service android:name=".SecondService"></service> 26 </application> 27 28 </manifest>
2.java
(1)MainActivity.java
1 package com.service2; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 13 import com.service2.SecondService.FirstBinder; 14 15 public class MainActivity extends Activity { 16 17 private Button bindBtn = null; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 bindBtn = (Button)findViewById(R.id.bindBtn); 23 bindBtn.setOnClickListener(new OnClickListener() { 24 @Override 25 public void onClick(View v) { 26 Intent intent = new Intent(); 27 intent.setClass(MainActivity.this, SecondService.class); 28 bindService(intent, conn, BIND_AUTO_CREATE); 29 } 30 }); 31 } 32 33 ServiceConnection conn = new ServiceConnection() { 34 @Override 35 public void onServiceDisconnected(ComponentName name) { 36 System.out.println("onServiceDisconnected>>>>>>"); 37 } 38 @Override 39 public void onServiceConnected(ComponentName name, IBinder service) { 40 FirstBinder binder = (FirstBinder)service; 41 System.out.println("onServiceConnected>>>>>"+binder.getData()); 42 } 43 }; 44 }
(2)SecondService.java
1 package com.service2; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class SecondService extends Service { 9 10 @Override 11 public IBinder onBind(Intent intent) { 12 IBinder binder = new FirstBinder(); 13 return binder; 14 } 15 16 class FirstBinder extends Binder { 17 public String getData() { 18 return "test data"; 19 } 20 } 21 }
You can do anything you set your mind to, man!