AIDL Service //跨进程调用Service
    一个APK想调用另一个APK中的Service 如何实现
AIDL //定义两个进程之间的通信接口

Demo1 传递简单数据

AidlService

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();
} }
package com.example.aidlservice;

interface ICat
{
String getColor();
double getWeight();
}

AidlClient

public class MainActivity 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.activity_main);
get = (Button) findViewById(R.id.get);
color = (EditText) findViewById(R.id.color);
weight = (EditText) findViewById(R.id.weight);
// 创建所需绑定的Service的Intent
Intent intent = new Intent();
intent.setAction("com.example.aidlservice.AIDL_SERVICE");
// 绑定远程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);
} }
package com.example.aidlservice;

interface ICat
{
String getColor();
double getWeight();
}

最新文章

  1. 自定义ConfigSection
  2. 微软Visual Studio Code 0.8.0发布,新增多种主题
  3. hrbrid需要做的
  4. .net, java, c/c++ 和钱
  5. Python~list,tuple^_^dict,set
  6. PHP部分字符串函数汇总
  7. MassTransit_契约的创建
  8. jQuery修改后代、兄弟样式
  9. extjs笔记
  10. Java API —— Set接口 & HashSet类 & LinkedHashSet类
  11. 使用 Infragistics 的 NetAdvantage 组件时替换部分菜单语言的方法
  12. C语言学习笔记(二):指针的用法
  13. Hibernate工作流程
  14. twisted是python实现的基于事件驱动的异步网络通信构架。
  15. POJ 1182 食物链 -- 解题报告
  16. Python-re模块中一些重要函数
  17. #019 还未搞明白的C语言问题
  18. yii2.0 curd操作
  19. 使用log
  20. 【T04】开发并使用应用程序框架

热门文章

  1. C++陷阱之慎用string类
  2. 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏
  3. Unity-Animator深入系列---FAQ
  4. HDU(1175),连连看,BFS
  5. Poj(2367),拓扑排序
  6. 放松时刻——C#分割字符串
  7. php使用 set_include_path
  8. datagrid后台分页js.js
  9. Struts2的标签库(三)——控制标签
  10. C#支持文件拖拽