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. sdp内容解析

    sdp解释 http://datatracker.ietf.org/doc/draft-nandakumar-rtcweb-sdp/?include_text=1

  2. A Round Peg in a Ground Hole(凸包应用POJ 1584)

    A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5684 Accepte ...

  3. 逆序数还原(FZU)

    逆序数还原 Accept: 244 Submit: 451 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 有一段时 ...

  4. python学习笔记-day4笔记 常用内置函数与装饰器

    1.常用的python函数 abs             求绝对值 all               判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any          ...

  5. Poj(1274),二分图匹配

    题目链接:http://poj.org/problem?id=1274 The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  6. js遍历

    最近看了一些不错的文章关于js遍历+js数组去重+文件上传的,今天也自己动手试了试.有好多之前不是细节不是很了解.正好学习了. map函数也是 类似这样的对象还有函数的属性arguments对象,当然 ...

  7. Python安装指南

    说明:我的安装环境是centos6.4 ,32位系统:(#号之后内容为注释说明内容) 1.准备 centos是自带python的,所以可以在shell下直接执行:python 可以看到相应的打印信息, ...

  8. 编译android源码官方教程(5)编译完之后刷机、编译fastboot

    Running Builds IN THIS DOCUMENT Building fastboot and adb Booting into fastboot mode Unlocking the b ...

  9. C语言中'\0'与'\n'

    '\0'表示ASCII编号为0的字符,在C语言中最常用于代表字符串结束的标志.'\n'表示ASCII编号为13的字符,代表回车键,输出这个字符就会换一行. '\0'作为字符串的结束标志,本身会占用一个 ...

  10. Listview的闪烁问题

    在更新Listview数据时会出现闪烁,主要原因是没有开启双缓冲属性,在C#中可以通过重载Listview的方式开启,代码如下: using System.Windows.Forms; public ...