一、基本类型

1、AIDL是什么

AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写,对于小白来说,AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的APP可以和其他APP交互。

2、AIDL的使用

因为是两个app交互,所以需要创建两个app。

1.创建AIDL文件

在app项目创建aidl文件

输入名称后,as就帮我们创建了一个AIDL文件。

上面就是as帮我生成的aidl文件。basicTypes这个方法可以无视,看注解知道这个方法只是告诉你在AIDL中你可以使用的基本类型(int, long, boolean, float, double, String),因为这里是要跨进程通讯的,所以不是随便你自己定义的一个类型就可以在AIDL使用的,这些后面会说。我们在AIDL文件中定义一个我们要提供给第二个APP使用的接口。

定义好之后,就可以sycn project一下,然后新建一个service。在service里面创建一个内部类,继承你刚才创建的AIDL的名称里的Stub类,并实现接口方法,在onBind返回内部类的实例。

import android.os.RemoteException;

public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new MyBinder();
} class MyBinder extends Person.Stub { @Override
public String getName() throws RemoteException {
return "456";
}
}
}

接下来,将我们的AIDL文件拷贝到第二个项目(aidlcalldemo),然后sycn project一下工程。

注意:这边的包名要跟第一个项目的一样哦,这之后在Activity中绑定服务。

package com.lyf.test.aidlcalldemo;

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.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast; import com.lyf.test.aidldemo.Person; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.button)
Button button;
private Person person; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.lyf.test.aidldemo", "com.lyf.test.aidldemo.MyService"));
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
person = Person.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) { }
}, BIND_AUTO_CREATE);
} @OnClick(R.id.button)
public void onViewClicked() {
try {
Toast.makeText(MainActivity.this, person.getName(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

Android5.0之后只能使用显式Intent启动Service组件。

在onServiceConnected方法中通过Person.Stub.asInterface(service)获取Person对象,然后在onClick中调用person.getName()。

二、自定义类型

如果我要在AIDL中使用自定义的类型,要怎么做呢。首先我们的自定义类型要实现Parcelable接口,下面的代码中创建了一个User类并实现Parcelable接口。这边就不对Parcelable进行介绍了,不熟悉的童鞋自行查找资料,总之我们这边可以借助studio的Show Intention Action(也就是Eclipse中的Quick Fix,默认是alt+enter键)帮我们快速实现Parcelable接口。
 
1、首先在一个包中创建java类

实现Parcelable接口

接下新建一个aidl文件,名称为我们自定义类型的名称,这边是User.aidl。

 

在User.aidl声明我们的自定义类型和它的完整包名,注意这边parcelable是小写的,不是Parcelable接口,一个自定类型需要一个这样同名的AIDL文件。

package com.lyf.test.aidldemo;
parcelable User;
然后再在我们的AIDL接口中导入我们的AIDL类型。

然后定义接口方法,sycn project后就可以在service中做具体实现了。

package com.lyf.test.aidldemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new MyBinder();
} class MyBinder extends IMyAidlInterface.Stub { @Override
public String getName() throws RemoteException {
return "456";
} @Override
public User getUserName() throws RemoteException {
return new User("789");
}
}
}

最后将我们的AIDL文件和自定义类型的java一并拷贝到第二个项目,注意包名都要一样哦。

然后就可以在Activity中使用该自定义类型的AIDL接口了。

package com.lyf.test.aidlcalldemo;

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.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast; import com.lyf.test.aidldemo.IMyAidlInterface; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.button)
Button button;
private IMyAidlInterface iMyAidlInterface; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.lyf.test.aidldemo", "com.lyf.test.aidldemo.MyService"));
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) { }
}, BIND_AUTO_CREATE);
} @OnClick(R.id.button)
public void onViewClicked() {
try {
Toast.makeText(MainActivity.this, iMyAidlInterface.getUserName().getName(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

大功告成。

github项目demo:https://github.com/First-Time/AIDLDemo.git

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

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

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

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

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

  3. AIDL跨进程通信

    Android跨进程通信会用到AIDL,当然跨进程通信不一定要用AIDL,像广播也是可以的,当然这里用到AIDL相对比较安全一些: AIDL允许传递基本数据类型(Java 的原生类型如int/long ...

  4. Android中的跨进程通信方法实例及特点分析(二):ContentProvider

    1.ContentProvider简单介绍 在Android中有些数据(如通讯录.音频.视频文件等)是要供非常多应用程序使用的.为了更好地对外提供数据.Android系统给我们提供了Content P ...

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

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

  6. Android四种跨进程通信

    由于android系统中应用程序之间不能共享内存.因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一些.在android SDK中提供了4种用于跨进程通讯的方式.这4种方式正好对应于andro ...

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

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

  8. android自动化测试解决跨进程通信问题

    大概用这些吧: IPC  Handler    Messager   Bundle  service(binder)   messageconnection ,thead.getXXX.getId 注 ...

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

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

随机推荐

  1. 浅谈js抽象工厂模式

    一.简单工厂 定义:简单工厂模式中,可以根据参数的不同返回不同类的实例.简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 比如你去专门卖鼠标的地方你可以买各种各样的 ...

  2. 关于弹性布局的 flex-grow的用法和flex-shrink的用法

    1.首先 flex-grow设置在子项目上 2.flex-grow默认值为0,如果为值1的时候就会撑满 3.flex-grow还可以给其中的一个子元素单独设置,设置为2,其它的则为1或者2都可以,具体 ...

  3. 微信小程序之----制作视频弹幕

    1. 文件目录     使用微信, 长度单位使用 rpx 可以避免不同设备的样式调试问题     经验总结,之前一直使用px ,发现换了测试机就崩了        2. index.wxml页面设置v ...

  4. hibernate save数据的时候报错:ids for this class must be manually assigned before calling save()

    这个错误是因为eclipse  这种jpatools 自动生成的实体类内没把id 设置为自增,还有id的值在生成的时候默认为string 即使加上了也所以无法自增 ,所以还需要把string 换成In ...

  5. 特殊篮子问题——C语言暴力破解

    You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of th ...

  6. 五子棋 AI(AIpha-beta算法)

    博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...

  7. redis 一些操作命令

    # 删除laravel keyredis-cli -h 10.9.103.15 -a password keys "laravel*" | xargs redis-cli -h 1 ...

  8. boost asio 学习(一)io_service的基础

    原文  http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...

  9. 用python语言算π值并且带有进度条

    用python算圆周率π 1.准备第三方库pip 打开cmd 输入代码:pip install requests ,随后就会成功 因为小编已经安装好了,所以就不把图截出来了 2.利用马青公式求π    ...

  10. javascript字符串方法总结

    一.单引号字符串内部可以使用双引号,双引号字符串内部也可以使用单引号 "hello 'world'" 'welcome "to" js' 二.多行和转义 如果要 ...