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();
}

Android AIDL Service的更多相关文章

  1. Android AIDL SERVICE 双向通信 详解

    http://www.cnblogs.com/punkisnotdead/p/5062631.html 起因 是这个blog 提到了 用webview 的时候 用开启子进程的方式 可以极大避免内存泄露 ...

  2. Android AIDL Service 跨进程传递复杂数据

    黑夜 黑夜给了我黑色的眼睛,我却用它寻找光明~ 传值方式 AIDL是同意跨进程传递值的,一般来说有三种方式: - 广播:这样的算是比較常见的一种方式了,传递小数据不错 - 文件:这个是保存到文件里.然 ...

  3. Android studio 中创建AIDL Service

      1.概述  AIDL在android系统中的作用 AIDL,Android Interface definition language的缩写,它是一种android内部进程通信接口的描写叙述语言, ...

  4. 一个简单的demo学习Android远程Service(AIDL的使用)

    这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...

  5. android 远程Service以及AIDL的跨进程通信

    在Android中,Service是运行在主线程中的,如果在Service中处理一些耗时的操作,就会导致程序出现ANR. 但如果将本地的Service转换成一个远程的Service,就不会出现这样的问 ...

  6. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  7. android 多进程 Binder AIDL Service

    本文參考http://blog.csdn.net/saintswordsman/article/details/5130947 android的多进程是通过Binder来实现的,一个类,继承了Bind ...

  8. Android中的跨进程通信方法实例及特点分析(一):AIDL Service

    转载请注明出处:http://blog.csdn.net/bettarwang/article/details/40947481 近期有一个需求就是往程序中增加大数据的採集点,可是由于我们的Andro ...

  9. 【转载】Android Studio Service AIDL 详解

    公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...

随机推荐

  1. [充电][ios]ios充电接口

    知乎推荐:http://www.zhihu.com/topic/19693633/top-answers 外文教学网站: http://www.raywenderlich.com/ 著作权归作者所有. ...

  2. 使用Texture2D创建Cubemap

    网上有很多,但大多使用Camera.RenderToCubemap接口,不能满足需求. 写了段代码可以载入Texture2D生成Cubemap(在Editor下运行): /// <summary ...

  3. 驱动之module_init/module_exit

    在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...

  4. WF3.0 CallExternalMethod使用技巧

    CallExternalMethod用于工作流向宿主进程中通信 简单的介绍一下它的使用技巧,参照网上的一个questioner源码进行了改进,因为我感觉这个源码提供的通信demo过于繁琐. 看看ser ...

  5. Struts2应用的开发流程

    Struts2的开发流程 为了能够在eclipse中使用Struts2在进行开发时,需要根据需要导入一些有关的jar包: 在官网下载相关的压缩包,这里下载了两个:struts-2.3.30-all.z ...

  6. CodeForces 651A Joysticks 贪心

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. windows8运行php Composer出现SSL报错的问题

    这是没有安装CA证书导致的!!! CA证书下载地址:http://curl.haxx.se/docs/caextract.html 然后修改php.ini文件 openssl.cafile= D:/w ...

  8. 双4G LTE

    我们即将上市的Xplay3S将支持双4G LTE(TDD-LTE+FDD-LTE),那什么是4G LTE呢,今天来给大家科普下4G LTE网络知识. 什么是TDD-LTE/FDD-LTE? LTE是通 ...

  9. HDU 4441 Queue Sequence(splay)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并 ...

  10. excel在一个图表内,显示折线图和柱状图

      折线图和柱状图,在同一个图表中拆分显示   一个图,设置主坐标轴 另外一个图,设置次坐标轴     拆分,通过调整纵坐标的最小值和最大值来实现     关于图表的标题,选中图表,选择布局,然后图表 ...