A. android 客戶端怎樣使用aidl對接服務端
使用統一的AIDL介面,必須保證包名,介面名,介面定義都一致,最好採用直接復制。
客戶端:利用Context,intent實現對Serivce的綁定和調用。
其中service中的android:name為介面的實現類所在位置。intent-filter為AIDL介面文件所在位置。在客戶端發起bind時,發送的Intent應該與intent-filter中android:name指定一致。否則會出現無法找到該介面。
IntentUIntent=newIntent("com.ipanel.upgrade.UpgradeSystem");
bindService(UIntent,Uconn,BIND_AUTO_CREATE);
使用在線伺服器編譯AIDL可能會出現問題:
【1】NoAndroid.mkinpackages/apps/UpgradeService.
需要在客戶端和服務端的工程下添加Andorid.mk文件。
【2】無法找到該AIDL文件的聲明。
需要在服務端的Android.mk中添加對AIDL的編譯。
LOCAL_SRC_FILES:=$(callall-subdir-java-files)
src/com/ipanel/properties/Properties.aidl
src/com/ipanel/upgrade/UpgradeSystem.aidl
B. Android中怎麼啟動關閉Service及功能解釋
Service 的啟動方式有兩種:Context.startService() , Context.bindService()。分別對應的關閉方式為:stopService 和unbindService. 它主要用於在後台處理一些耗時的邏輯,或者去執行某些需要長期運行的任務。必要的時候我們甚至可以在程序退出的情況下,讓Service在後台繼續保持運行狀態。
常用的Service 有本地服務(LocalService)和遠程服務(RemoteService)。
遠程服務AIDL 可以實現進程間的通訊,跨應用交互。
C. 怎麼添加靜態service android
說明:本次說的系統服務,是指捆綁在system_process進程中的服務。
仔細閱讀源碼可以發現,在frameworks/base/services/java/com/android/server/SystemServer.java中添加了很多服務,什麼熵信息、電量、wife等等服務,這些服務啟動都在launcher之前,一般要繼承Binder或者繼承一個AIDL。下面試著來添加一個簡單系統服務
一。在frameworks/base/core/java/android/os/增加一個aidl文件,最後用aidl工具生產一個Java文件,這樣可以方便後面:
D. android怎麼寫aidl文件
建立AIDL服務要比建立普通的服務復雜一些,具體步驟如下:
(1)在Eclipse Android工程的Java包目錄中建立一個擴展名為aidl的文件。該文件的語法類似於Java代碼,但會稍有不同。詳細介紹見實例52的內容。
(2)如果aidl文件的內容是正確的,ADT會自動生成一個Java介面文件(*.java)。
(3)建立一個服務類(Service的子類)。
(4)實現由aidl文件生成的Java介面。
(5)在AndroidManifest.xml文件中配置AIDL服務,尤其要注意的是,<action>標簽中android:name的屬性值就是客戶端要引用該服務的ID,也就是Intent類的參數值。
建立AIDL服務
本例中將建立一個簡單的AIDL服務。這個AIDL服務只有一個getValue方法,該方法返回一個String類型的值。在安裝完服務後,會在客戶端調用這個getValue方法,並將返回值在TextView組件中輸出。建立這個AIDL服務的步驟如下:
(1)建立一個aidl文件。在Java包目錄中建立一個IMyService.aidl文件。IMyService.aidl文件的位置如圖
IMyService.aidl文件的內容如下:
Java代碼:
package eoe.demo;
interface IMyService {
String getValue();
}
IMyService.aidl文件的內容與Java代碼非常相似,但要注意,不能加修飾符(例如,public、private)、AIDL服務不支持的數據類型(例如,InputStream、OutputStream)等內容。
(2)如果IMyService.aidl文件中的內容輸入正確,ADT會自動生成一個IMyService.java文件。讀者一般並不需要關心這個文件的具體內容,也不需要維護這個文件。關於該文件的具體內容,讀者可以查看本節提供的源代碼。
(3)編寫一個MyService類。MyService是Service的子類,在MyService類中定義了一個內嵌類(MyServiceImpl),該類是IMyService.Stub的子類。MyService類的代碼如下:
Java代碼:
package eoe.demo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
public class MyServiceImpl extends IMyService.Stub {
@Override
public String getValue() throws RemoteException {
return "Android/OPhone開發講義";
}
}
@Override
public IBinder onBind(Intent intent) {
return new MyServiceImpl();
}
}
在編寫上面代碼時要注意如下兩點:
IMyService.Stub是根據IMyService.aidl文件自動生成的,一般並不需要管這個類的內容,只需要編寫一個繼承於IMyService.Stub類的子類(MyServiceImpl類)即可。
onBind方法必須返回MyServiceImpl類的對象實例,否則客戶端無法獲得服務對象。
(4)在AndroidManifest.xml文件中配置MyService類,代碼如下:
Java代碼:
<service android:name=".MyService" >
<intent-filter>
<action android:name="net.blogjava.mobile.aidl.IMyService" />
</intent-filter>
</service>
下面來編寫客戶端的調用代碼。首先新建一個Eclipse Android工程(ch08_aidlclient),並將自動生成的IMyService.java文件連同包目錄一起復制到ch08_aidlclient工程的src目錄中,如圖所示。
調用AIDL服務首先要綁定服務,然後才能獲得服務對象,代碼如下:
Java代碼:
package net.blogjava.mobile;
import net.blogjava.mobile.aidl.IMyService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; import android.os.IBinder;
import android.view.View; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.TextView;
public class Main extends Activity implements OnClickListener {
private IMyService myService = null;
private Button btnInvokeAIDLService;
private Button btnBindAIDLService;
private TextView textView;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 獲得服務對象
myService = IMyService.Stub.asInterface(service);
btnInvokeAIDLService.setEnabled(true);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBindAIDLService:
// 綁定AIDL服務
bindService(new Intent("net.blogjava.mobile.aidl.IMyService"), serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.btnInvokeAIDLService:
try {
textView.setText(myService.getValue());
// 調用服務端的getValue方法
} catch (Exception e) {
}
break;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);
btnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService); btnInvokeAIDLService.setEnabled(false);
textView = (TextView) findViewById(R.id.textview);
btnInvokeAIDLService.setOnClickListener(this);
btnBindAIDLService.setOnClickListener(this);
}。
E. 如何用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的相應數據。
F. android的aidl文件提示出錯,怎麼回事
你檢查一下aidl文件
G. android music 播放service為什麼aidl
bindservice是與activity綁定的。如果acitivity退出的話,service也就銷毀了,無法後台播放。當然,可以用startservice
H. android 怎樣用AIDL Service 傳遞復雜數據
第一步:部署我們的服務端,也就是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的相應數據。
I. 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 上實現連接、斷開、數據通信