当前位置:首页 » 安卓系统 » androidaidl的使用

androidaidl的使用

发布时间: 2023-08-02 21:47:35

1. android studio创建aidl文件parcelable类怎么创建

1、要进行创建aidl的接口之前,需要进行选中当前的项目的文件中,选中完成项目之后。
2、然后进行点击android
studio菜单中的“file”的选项,就会弹出了一个下拉的菜单中“new”选项。
3、就会弹出了一个下一级的菜单中,进行选择下一级菜单中的“aidl”的选项。
4、就会弹出了一个菜单中,进行点击这个菜单中的“aidl
file”的选项。
5、就会弹出了一个aidl的窗口的选项,需要给接口进行添加名称,在interface
name的输入框中进行输入名称,点击”finish“的选项。
6、然后在项目中进行天机了一个aidl的文件,在该文件下中,有一个aidl的后缀的文件,其实就是一个接口文件。

2. 如何用Android Service通过aidl传递一个数据流

第一步:部署我们的服务端,也就是Service端:

1:在Service端我先自定义2个类型:Person和Pet。因为我们需要跨进程传递Person对象和Pet对象,所以Person类和Pet类都必须实现Parcelable接口,并要求在实现类中定义一个名为CREATER,类型为Parcelable.creator的静态Field。

代码如下:

这是我Service端的部署情况(其中MainActivity可以不用去实现,因为我们只提供服务,没有窗口显示):

第二步:部署客户端:

1.在客户端新建一个包,命名需要和服务端放置aidl文件的包名相同(我这里是com.example.remoteservice),然后把服务端的Person.java,Pet.java,Person.aidl,Pet.aidl,IPet.aidl复制到这个包下面

2.在activity中绑定远程服务进行数据交换,layout布局和activity代码如下:

1 <RELATIVELAYOUT xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" tools:context="com.example.remoteclient.RemoteClient" android:paddingtop="@dimen/activity_vertical_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:layout_width="match_parent" android:layout_height="match_parent" 9="" 8="" 7="" 6="" 5="" 4="" 3="" 2="">

10

11 <LINEARLAYOUT android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 14="" 13="" 12="">

15

16 <LINEARLAYOUT android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" 19="" 18="" 17="">

20

21 <EDITTEXT android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:id="@+id/editText_person" android:ems="10" 26="" 25="" 24="" 23="" 22="">

27 </EDITTEXT>

28

29<BUTTON type=submit android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:id="@+id/button_ok" android:text="确定" 34="" 33="" 32="" 31="" 30="">

35

36

37 <LISTVIEW android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView_pet" 40="" 39="" 38="">

41 </LISTVIEW>

42

43

44</BUTTON></LINEARLAYOUT></LINEARLAYOUT></RELATIVELAYOUT>


1 package com.example.remoteclient;

2

3 import android.app.Service;

4 import android.content.ComponentName;

5 import android.content.Intent;

6 import android.content.ServiceConnection;

7 import android.os.Bundle;

8 import android.os.IBinder;

9 import android.os.RemoteException;

10 import android.support.v7.app.ActionBarActivity;

11 import android.util.Log;

12 import android.view.View;

13 import android.view.View.OnClickListener;

14 import android.widget.ArrayAdapter;

15 import android.widget.Button;

16 import android.widget.EditText;

17 import android.widget.ListView;

18

19 import com.example.remoteservice.IPet;

20 import com.example.remoteservice.Person;

21 import com.example.remoteservice.Pet;

22

23 import java.util.List;

24

25 public class RemoteClient extends ActionBarActivity {

26

27 public static final String REMOTE_SERVICE_ACTION = com.example.remoteservice.RemoteService.ACTION;

28 EditText editText;

29 Button button;

30 ListView listView;

31

32 IPet petService;// 声明IPet接口

33 List<PET> pets;

34 ServiceConnection conn = new ServiceConnection() {

35

36 @Override

37 public void onServiceDisconnected(ComponentName name) {

38 Log.i(csx, onServiceDisconnected);

39 conn = null;

40 }

41

42 @Override

43 public void onServiceConnected(ComponentName name, IBinder service) {

44 Log.i(csx, onServiceConnected);

45 petService = IPet.Stub.asInterface(service);// 通过远程服务的Binder实现接口

46

47 }

48 };

49

50 @Override

51 protected void onCreate(Bundle savedInstanceState) {

52 super.onCreate(savedInstanceState);

53 setContentView(R.layout.remote_client_layout);

54 editText = (EditText) findViewById(R.id.editText_person);

55 button = (Button) findViewById(R.id.button_ok);

56 listView = (ListView) findViewById(R.id.listView_pet);

57

58 Intent service = new Intent();

59 service.setAction(REMOTE_SERVICE_ACTION);

60

61 bindService(service, conn, Service.BIND_AUTO_CREATE);// 绑定远程服务

62

63 button.setOnClickListener(new OnClickListener() {

64

65 @Override

66 public void onClick(View v) {

67 String personName = editText.getText().toString();

68 if (personName == null || personName.equals()) {

69

70 return;

71 }

72

73 try {

74 pets = petService.getPets(new Person(1, personName, personName));// 调用远程service的getPets方法

75 updataListView();

76

77 } catch (RemoteException e) {

78

79 e.printStackTrace();

80 } catch (NullPointerException e) {

81 e.printStackTrace();

82 }

83

84 }

85 });

86

87 }

88

89 public void updataListView() {

90 listView.setAdapter(null);

91

92 if (pets == null || pets.isEmpty()) {

93 return;

94

95 }

96 ArrayAdapter<PET> adapter = new ArrayAdapter<PET>(RemoteClient.this,

97 android.R.layout.simple_list_item_1, pets);

98 listView.setAdapter(adapter);

99

100 }

101

102 @Override

103 protected void onDestroy() {

104

105 unbindService(conn);// 解除绑定

106 super.onDestroy();

107 }

108

109 }</PET></PET></PET>

到此为止所有的工作都完成了,下面我们看一下效果:我在编辑框中输入“csx”,点击确定,就会显示出服务端RemoteService中pets的相应数据。

3. Android:AIDL进程间通信基本框架

在某些业务场景下,我们需要在应用中单独开启一个进程进行一些操作。比如性能监控,如果让原始业务和性能监控本身的业务跑在同一个进程下,那么就会导致性能统计的数据的失真。

而进程间通信,一般采用AIDL机制的客户端与服务端通信。

AIDL只能传递如下几类数据:

当传递自定义 Parcelable 时,有三处地方需要注意:

当传递其他 aidl 接口时,同样必须要 import 这个 aidl 文件

编写完 aidl 文件后,make一下工程,会在 build 下的 generated 下的 source 下的 aidl 目录生成对应的接口类文件。aidl 接口其实就是 API 接口,通过实现对应接口类的 Stub 子类来实现具体的 API 逻辑;通过对应接口类的 Stub 子类的 asInterface 方法得到具体的实现类,调用具体的 API 方法。

一个基本的客户端服务端的通信结构一般包括如下功能

客户端的功能

服务端的功能

客户端的相关功能实现比较简单,麻烦的是服务端的功能。因为 AIDL 接口定义的都是服务端的接口,是由客户端来调用的。而想要实现服务端反向调用客户端则需要通过其他手段实现。

想要实现服务端主动连接客户端,最好的办法就是 服务端发送广播,客户端收到广播后再主动连接服务端 ,通过这种方式变相地实现服务端主动连接客户端的功能

想要实现服务端主动断开客户端,除了上面 发送广播是一种实现方式外,还可以通过 android 的系统API RemoteCallbackList,用包名作为key值来注册远程回调接口的方式,让服务端持有客户端的回调接口,服务端调用回调接口,客户端在回调接口中实现主动断开服务端 ,通过这种方式变量地实现服务端主动断开客户端的功能。而采用后者会显得更加优雅

既然所有的操作归根结底都是由客户端来完成的,那么客户端必须得有如下的功能模块:

服务端必须得有的功能模块:

那么,整体的通信流程就是如下的步骤:

首先是通信的 aidl 接口定义

然后是客户端的连接操作与断开连接操作,包括广播接收者的注册以及回调接口的实现

然后是客户端的拉取数据和推送数据操作

接着是服务端的 iBinder 接口的实现,完成回调接口的注册、业务子线程的开启和关闭、数据的推送和数据的拉取操作

然后是服务端的主动连接和主动断开连接操作

最后是服务端的 onUnbind 方法的实现,对回调接口进行反注册

服务端模仿 FloatViewPlugin 自定义插件,实现 IServicePlugin 接口,定制个性化的悬浮窗插件

客户端在 Appliaction 的 onCreate方法中初始化

在 MainActivity 上实现连接、断开、数据通信

4. 安卓IPC跨进程通讯:AIDL+Retrofit——AndLinker的初步使用

需要用到安卓跨进程通讯,明指IPC (进程间通信) 的时候,AndLinker是一款Android上的IPC (进程间通信) 库,结合了 AIDL 和 Retrofit 的诸多特性,且可以与 RxJava 和 RxJava2 的Call Adapters无缝结合使用。

个人简单理解就是:简化AIDL流程的一个第三方库。使用时斗昌需要先了解一下AIDL、retrofit。

以普通Java接口代替AIDL接口

像 Retrofit 一样生成远程服务接口的IPC实现

支持的Call Adapters:Call, RxJava  Observable, RxJava2  Observable & Flowable

支持远程服务回调机制

支持AIDL的所有数据类型

支持AIDL的所有数据定向tag:in,out,inout

支持AIDL的oneway关键字

在服务端以及客户端的项目根目录的build.gradle中添加jcenter()仓库

在App的build.gradle中添加如下依赖

AndLinker支持AIDL所有数据类型:

Java语言中的所有原始类型 (如:int,long,char,boolean,等等)

String

CharSequence

Parcelable

List (List中的所有元素必须是此列表中支持的数据类型)

Map (Map中的所有元素必须是此列表中支持的数据类型)

接口里的方法就是按需激销配求需创建。这里只举几个简单的示例。

热点内容
笔记本电脑如何设置锁屏密码 发布:2025-02-04 13:54:42 浏览:161
构成c语言程序的基本单位 发布:2025-02-04 13:49:53 浏览:988
如何修改已经更改的密码 发布:2025-02-04 13:38:38 浏览:773
唐dm2021买哪个配置划算 发布:2025-02-04 13:38:38 浏览:627
真空压缩重 发布:2025-02-04 13:38:37 浏览:640
alias脚本 发布:2025-02-04 13:38:03 浏览:740
linux终端字符 发布:2025-02-04 12:52:40 浏览:737
c语言程序设计mobi 发布:2025-02-04 12:51:55 浏览:260
rsa算法c语言 发布:2025-02-04 12:50:36 浏览:786
阿里云服务器托管破解 发布:2025-02-04 12:47:43 浏览:258