导航:首页 > 操作系统 > androidserviceaidl

androidserviceaidl

发布时间:2022-11-01 16:06:52

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 上实现连接、断开、数据通信

阅读全文

与androidserviceaidl相关的资料

热点内容
pboc长度加数据加密 浏览:185
英雄联盟国际服手游怎么下安卓 浏览:297
程序员的思路 浏览:234
只能用命令获得的四种方块 浏览:358
怎么用命令方块防止开创造 浏览:807
扫描版的pdf 浏览:790
编程猫怎样做3d游戏 浏览:207
怎么查找云服务器上的ftp 浏览:156
我的世界服务器如何注册账号 浏览:934
统计英文字符python 浏览:423
linux信息安全 浏览:908
压缩机接线柱爆 浏览:999
程序员自主创业 浏览:584
汇编程序员待遇 浏览:359
怎么批量有顺序的命名文件夹 浏览:211
杭州程序员健身 浏览:19
dvd光盘存储汉子算法 浏览:758
苹果邮件无法连接服务器地址 浏览:963
phpffmpeg转码 浏览:672
长沙好玩的解压项目 浏览:145