Android跨进程通信会用到AIDL,当然跨进程通信不一定要用AIDL,像广播也是可以的,当然这里用到AIDL相对比较安全一些;

AIDL允许传递基本数据类型(Java 的原生类型如int/long/char/boolean/double/float/String、CharSequence、List和Map、)、实现android.os.Parcelable 接口的对象类

两个实例来验证一下AIDL

1.创建一个提供AIDL服务的AIDL_Test_Server,新建AIDL接口IRemoteService,在接口中定义方法

package cn.jsonlu.aidl.server;

/**
*远程AIDL服务接口
**/
interface IRemoteService { /**
*定义接口方法
*/
int add(int a,int b);
}

2.在AIDL_Test_Server中创建服务

package cn.jsonlu.aidl.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; /**
* Author:JsonLu
* DateTime:2016/2/25 10:59
* Email:jsonlu@qq.com
* Desc:
**/
public class RemoteService extends Service { private IBinder iBinder = new IRemoteService.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}; @Override
public IBinder onBind(Intent intent) {
return iBinder;
}
}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.jsonlu.aidl.server"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".RemoteService"
android:exported="true">
<intent-filter>
<action android:name="cn.jsonlu.aidl.server.IRemoteService" />
</intent-filter>
</service>
</application> </manifest>

3.创建一个使用AIDL服务的AIDL_Test_Client,将AIDL_Test_Server中的.aidl文件复制到AIDL_Test_Client中(接口包名必须一致)

package cn.jsonlu.aidl.client;

import android.app.Activity;
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.widget.EditText; import cn.jsonlu.aidl.server.IRemoteService; public class MainActivity extends Activity { private EditText a, b, c;
private IRemoteService aidl;
private boolean bindFlag = false;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
aidl = IRemoteService.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) {
aidl = null;
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bindService();
} private void init() {
a = (EditText) findViewById(R.id.num_a);
b = (EditText) findViewById(R.id.num_b);
c = (EditText) findViewById(R.id.num_c);
} public void onClick(View v) {
if (!bindFlag) {
bindService();
} else {
int sum = 0;
try {
sum = aidl.add(Integer.parseInt(a.getText().toString()), Integer.parseInt(b.getText().toString()));
} catch (RemoteException e) {
System.out.println("AIDL错误");
}
c.setText(String.valueOf(sum));
}
} private void bindService() { Intent intent = new Intent(IRemoteService.class.getName());
intent.setClassName("cn.jsonlu.aidl.server", "cn.jsonlu.aidl.server.RemoteService");
bindFlag = bindService(intent, conn, BIND_AUTO_CREATE);
if (!bindFlag) {
System.out.println("AIDL未绑定成功");
} /*
//打开其他APP页面
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("cn.jsonlu.aidl.server", "cn.jsonlu.aidl.server.MainActivity");
intent.setComponent(cn);
startActivity(intent);
*/ }
}

4.AIDL传递对象需要实现Parcelable接口

新建Bean实体类

package cn.jsonlu.aidl.server;

import android.os.Parcel;
import android.os.Parcelable; /**
* Author:JsonLu
* DateTime:2016/2/25 13:33
* Email:jsonlu@qq.com
* Desc:
**/
public class Person implements Parcelable { private int age;
private String name; public Person(int age, String name) {
this.age = age;
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} protected Person(Parcel in) {
readFromParcel(in);
} public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
} @Override
public Person[] newArray(int size) {
return new Person[size];
}
}; @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
} //注意读取变量和写入变量的顺序应该一致 不然得不到正确的结果
public void readFromParcel(Parcel source) {
name = source.readString();
age = source.readInt();
}
}

新建Person.aidl文件

// Person.aidl
package cn.jsonlu.aidl.server;
parcelable Person;

修改IRemoteService.aidl文件

// IRemoteService.aidl
package cn.jsonlu.aidl.server;
import cn.jsonlu.aidl.server.Person;
// Declare any non-default types here with import statements interface IRemoteService {
//计算两数的和
int add(int a,int b);
//传递对象
Person getPerson();
}

AIDL跨进程通信的更多相关文章

  1. Android使用AIDL跨进程通信

    一.基本类型 1.AIDL是什么 AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definiti ...

  2. Aidl跨进程通信机制-android学习之旅(87)

    Aidl简介 AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信的代码. 如果在 ...

  3. AIDL跨进程通信报Intent must be explicit

    在Android5.0机子上采用隐式启动来调试AIDL时,会出现Intent must be explicit的错误,原因是5.0的机子不允许使用隐式启动方式,解决的方法是:在启动intent时添加i ...

  4. 不依赖AIDL的跨进程通信

    http://blog.csdn.net/lmj623565791/article/details/38461079 如果知道AIDL和binder的原理,可以简单写一个不依赖AIDL的跨进程通信 不 ...

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

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

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

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

  7. 【朝花夕拾】跨进程通信,你只知道AIDL,就OUT了

    一.前言 提起跨进程通信,大多数人首先会想到AIDL.我们知道,用AIDL来实现跨进程通信,需要在客户端和服务端都添加上aidl文件,并在服务端的Service中实现aidl对应的接口.如果还需要服务 ...

  8. Android随笔之——跨进程通信(一) Activity篇

    在Android应用开发中,我们会碰到跨进程通信的情况,例如:你用QQ通讯录打电话的时候会调用系统的拨号应用.某些新闻客户端可以将新闻分享到QQ.微信等应用,这些都是跨进程通信的情况.简而言之,就是一 ...

  9. 跨进程通信之Messenger

    1.简介 Messenger,顾名思义即为信使,通过它可以在不同进程中传递Message对象,通过在Message中放入我们需要的入局,就可以轻松实现数据的跨进程传递了.Messenger是一种轻量级 ...

随机推荐

  1. 使用单调队列优化的 O(nm) 多重背包算法

    我搜索了一下,找到了一篇很好的博客,讲的挺详细:链接. 解析 多重背包的最原始的状态转移方程: 令 c[i] = min(num[i], j / v[i]) f[i][j] = max(f[i-1][ ...

  2. 程序员面试宝典题目重温-P1-100

    int f(int x ,int y){    return (x&y) + ((x^y)>>1)} f(729,271)输出是什么? x&y表示按位与,结果是x,y相同位 ...

  3. Cortex-M0系统滴答定时器Systick详解

    上图是LPC1114系统滴答定时器(SysTick)的结构图.系统滴答定时器位于Cortex-M0内核中,也就是说,不论是LPC1114,还是其他的Cortex-M0内核单片机,都有这个系统定时器.其 ...

  4. a trick in reading and storing file in the exact way!

    read and write file is a very common operation regarding file mainuplation. However, the powerfull g ...

  5. COJ 2124 Day8-例1

    Day8-例1 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 给定n.m的值,求

  6. Poetize4 创世纪

    3037: 创世纪 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 123  Solved: 66[Submit][Status] Description ...

  7. (转载)Mysql使用Describe命令判断字段是否存在

    (转载)http://www.jz123.cn/plus/view.php?aid=39200 工作时需要取得MySQL中一个表的字段是否存在 于是就使用Describe命令来判断 mysql_con ...

  8. android导入项目出错处理

    问题: 执行Import-Android下的Existing Android Code Into Workspace 解决方法:

  9. C# 匿名方法 委托 Action委托 Delegate委托

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿 ...

  10. [Locked] Shortest Word Distance I & II & III

    Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...