androidaidl的使用
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中的所有元素必須是此列表中支持的數據類型)
介面里的方法就是按需激銷配求需創建。這里只舉幾個簡單的示例。