Android使用AIDL跨进程通信

时间:2021-12-30 13:20:32

一、基本类型

1、AIDL是什么

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

2、AIDL的使用

因为是两个app交互,所以需要创建两个app。
Android使用AIDL跨进程通信

1.创建AIDL文件

在app项目创建aidl文件

Android使用AIDL跨进程通信

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

Android使用AIDL跨进程通信

Android使用AIDL跨进程通信

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

定义好之后,就可以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一下工程。

Android使用AIDL跨进程通信

注意:这边的包名要跟第一个项目的一样哦,这之后在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()。

Android使用AIDL跨进程通信

二、自定义类型

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

实现Parcelable接口

Android使用AIDL跨进程通信

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

 Android使用AIDL跨进程通信

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

package com.lyf.test.aidldemo;
parcelable User;
然后再在我们的AIDL接口中导入我们的AIDL类型。
Android使用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一并拷贝到第二个项目,注意包名都要一样哦。

Android使用AIDL跨进程通信

然后就可以在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. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. centos 安装php7.0.2

    PHP7.0正式版已经在2015年11月份左右发布,目前是PHP7.0.2版本,本人最早是从2015年8月php7的第一个测试版跟起,现在正式版发布. linux版本:64位CentOS 6.6 Ng ...

  3. Java NIO教程 MappedByteBuffer

    之前跟大家说过,要讲MappedByteBuffer,现在我来履行承诺了. 首先从大体上讲一下MappedByteBuffer究竟是什么.从继承结构上来讲,MappedByteBuffer继承自Byt ...

  4. == Got TLE on OJ? Here is the solution! ==

    As a solo warrior in OJ, I spent about nearly 50% of my time on tackling TLE - that is innumerous ho ...

  5. javascript 单行向上滚动文字

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  6. easyUI datagrid 动态绑定列名称

    easyUI 基于Jquery ,所以需要引用Jquery文件 easyUI自带了很多样式文件,可以根据需要,引用相应的css文件. 其中datagrid是一个根据json数据,js前端生成前端显示的 ...

  7. Angular - - angular&period;uppercase、angular&period;lowercase、angular&period;fromJson、angular&period;toJson

    angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...

  8. 【工具】Spring项目转化Spring Web项目插件

    前言 源于前一篇博文中提到,将Spring项目转化为Spring Web项目,发现添加项目文件和修改pom.xml文件等都是手动完成的,心想着开发一个Idea插件来自动化完成上面的过程,实现一键转化. ...

  9. OAuth2简易实战(二)-模拟客户端调用

    1. OAuth2简易实战(二) 1.1. 目标 模拟客户端获取第三方授权,并调用第三方接口 1.2. 代码 1.2.1. 核心流程 逻辑就是从数据库读取用户信息,封装成UserDetails对象,该 ...

  10. 使用Jmeter进行http接口测试(转载)

    原文:http://www.cnblogs.com/puresoul/p/4740436.html  前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于 ...