这节课很有难度,要勤奋学习!!
bound service 允许其它的组件(比如Activities)绑定到这个services上,可以发送请求,也可以接收请求,甚至进行进程间的通话,bound service仅仅在服务于其它组件时存在,不能独自无限期在后台运行.
一,创建Bound Services
当创建一个能提供绑定报务功能的服务时,我们必须提供一个IBinder对象,客户端能使用这个对象与服务进行进行交互,在Adroid中有三种方式定义:
1,扩展Binder类
2,使用Messenger
3,使用AIDL(Android Interface Definition Language)
二,通过扩展Binder类创建Bound Services
a,在Service类中,创建一个Binder实例
包含客户端能调用的公共方法
返回当前服务对象
b,在onBind()方法中返回Binder实例
c,在客户端,从onServiceConnected()方法中获得Binder实例
三,源码
BinderService.java
package com.example1.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class BinderService extends Service {
private static final String TAG = "BinderService";
private MyBinder binder = new MyBinder();//a,在Service类中,创建一个Binder实例
public class MyBinder extends BinderService
{
public BinderService getService()
{
return BinderService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return (IBinder) binder;//返回当前服务对象
}
public void MyMethod()//包含客户端能调用的公共方法
{
Log.i(TAG, "MyMethod");
}
}
BinderActivity.java
package com.example1.servicedemo;
import com.example1.servicedemo.BinderService.MyBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BinderActivity extends Activity {
private Button btnbinderservicestart;
private Button btnbinderservicestop;
private Boolean isConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.binder);
btnbinderservicestart=(Button)findViewById(R.id.btnbinderservicestart);
btnbinderservicestop=(Button)findViewById(R.id.btnbinderservicestop);
btnbinderservicestart.setOnClickListener(listener);
btnbinderservicestop.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnbinderservicestart:
bindService();
break;
case R.id.btnbinderservicestop:
unbind();
break;
default:
break;
}
}
};
private void unbind() {
if (isConnected) {
unbindService(conn);
}
}
private void bindService() {
Intent intent = new Intent(BinderActivity.this,BinderService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
isConnected=false;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
MyBinder myBinder = (MyBinder)binder;
BinderService service = myBinder.getService();
service.MyMethod();
isConnected=true;
}
};
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example1.servicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example1.servicedemo.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example1.servicedemo.BinderActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService"/>
<service android:name=".BinderService"/>
</application>
</manifest>