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. springMVC配置freemarker

    这里呢,我首先来说明一下写该篇的目的. 我最近要用到freemarker因此研究了一下这个东西. 先来说说如何配置吧. 1.jar包.地址见下链接. http://pan.baidu.com/s/1j ...

  2. DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)

    DBUtils:提高了程序的性能,编程更加简便 架包 mysql-connector-java-jar commons-dbcp-1.4jar commons-pool-1.5.5jar common ...

  3. AreYouBusy

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏

    Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...

  5. Unity-Animator深入系列---Foot IK

    回到 Animator深入系列总目录 最近在做一个demo,遇到了角色跑动不自然的问题(注意双腿): 后来得知勾选FootIK之后Unity会智能修复这类问题: 好像这个功能还能做到斜面地形匹配,不过 ...

  6. request、response 中文乱码问题与解决方式

    request乱码指的是:浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码:   response乱码指的是:服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码: ...

  7. eclipse字体推荐

    首先大家可以看看这里面推荐的最佳十款字体,http://www.iteye.com/news/11102-10-great-programming-font 但是经过测试发现,排名第一的字体在ecli ...

  8. sql 清空所有数据

    近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL S ...

  9. C语言使用fread和fwrite处理任何文件

    1.文件必须以二进制形式打开 FILE* pfile1=fopen("fileone","rb"); FILE* pfile2=fopen("file ...

  10. Python3基础 random 产生置顶区间的随机整数

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...