为了方便android系统中不同应用间的通信,android提供了aidl。客户端访某个应用的service时,将IBInder对象的代理传递给ServiceConnection的onServiceConnected方法的第二个参数。
一、服务端应用的实现
interface ICat
{
String getColor();
double getWeight();
}
/**
*
*/
package org.crazyit.service;
import java.util.Timer;
import java.util.TimerTask;
import org.crazyit.service.ICat.Stub;
import android.app.Service;
import android.content.Intent;
import
android.os.IBinder;
import android.os.RemoteException;
/**
* Description:
* <br/>网站: <a
href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright
(C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by
copyright laws.
* <br/>Program Name:
* <br/>Date:
*
@author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public
class AidlService extends Service
{
private CatBinder
catBinder;
Timer timer = new Timer();
String[] colors = new
String[]{
"红色",
"黄色",
"黑色"
};
double[] weights = new
double[]{
2.3,
3.1,
1.58
};
private String
color;
private double weight;
//
继承Stub,也就是实现额ICat接口,并实现了IBinder接口
public class CatBinder extends
Stub
{
@Override
public String getColor() throws
RemoteException
{
return color;
}
@Override
public
double getWeight() throws RemoteException
{
return
weight;
}
}
@Override
public void
onCreate()
{
super.onCreate();
catBinder = new
CatBinder();
timer.schedule(new
TimerTask()
{
@Override
public void run()
{
//
随机地改变Service组件内color、weight属性的值。
int rand = (int)(Math.random() *
3);
color = colors[rand];
weight =
weights[rand];
System.out.println("--------" +
rand);
}
} , 0 , 800);
}
@Override
public IBinder
onBind(Intent arg0)
{
/* 返回catBinder对象
*
在绑定本地Service的情况下,该catBinder对象会直接
* 传给客户端的ServiceConnection对象
*
的onServiceConnected方法的第二个参数;
* 在绑定远程Service的情况下,只将catBinder对象的代理
* 传给客户端的ServiceConnection对象
* 的onServiceConnected方法的第二个参数;
*/
return catBinder;
}
@Override
public void
onDestroy()
{
timer.cancel();
}
}
二、客户端应用访问方法
interface ICat
{
String getColor();
double getWeight();
}
package org.crazyit.client;
import org.crazyit.service.ICat;
import android.app.Activity;
import android.app.Service;
import
android.content.ComponentName;
import android.content.Intent;
import
android.content.ServiceConnection;
import android.os.Bundle;
import
android.os.IBinder;
import android.os.RemoteException;
import
android.view.View;
import android.view.View.OnClickListener;
import
android.widget.Button;
import android.widget.EditText;
/**
* Description: <br/>
* site: <a
href="http://www.crazyit.org">crazyit.org</a> <br/>
*
Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
* This program is
protected by copyright laws. <br/>
* Program Name: <br/>
*
Date:
*
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class AidlClient extends Activity
{
private ICat
catService;
private Button get;
EditText color, weight;
private
ServiceConnection conn = new
ServiceConnection()
{
@Override
public void
onServiceConnected(ComponentName name, IBinder service)
{
//
获取远程Service的onBind方法返回的对象的代理
catService =
ICat.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName
name)
{
catService = null;
}
};
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
get
= (Button) findViewById(R.id.get);
color = (EditText)
findViewById(R.id.color);
weight = (EditText)
findViewById(R.id.weight);
// 创建所需绑定服务的Intent
Intent intent = new
Intent();
intent.setAction("org.crazyit.aidl.action.AIDL_SERVICE");
//
绑定远程服务
bindService(intent, conn,
Service.BIND_AUTO_CREATE);
get.setOnClickListener(new
OnClickListener()
{
@Override
public void onClick(View
arg0)
{
try{
//
获取、并显示远程Service的状态
color.setText(catService.getColor());
weight.setText(catService.getWeight()
+ "");
}catch (RemoteException
e)
{
e.printStackTrace();
}
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
//
解除绑定
this.unbindService(conn);
}
}