android 怎样用AIDL Service 传递复杂数据

android 怎样用AIDL Service 传递复杂数据,第1张

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

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

2:创建完自定义类型之后还需要用AIDL来定义它们,Person.aidl和Pet.aidl的代码如下:

1 package com.example.remoteservice

2 parcelable Person

1 package com.example.remoteservice

2 parcelable Pet

3:完成1,2之后就可以使用AIDL定义通信接口了,在这里我定义一个IPet.aidl的接口,代码如下:

1 package com.example.remoteservice//必须导入包

2 import com.example.remoteservice.Person//指定自定义类的位置

3 import com.example.remoteservice.Pet

4

5 interface IPet

6 {

7 List getPets(in Person owner)//这里的in表示Person对象是输入的参数

8 }

4:服务端的最后一步就是实现Service了,当然不要忘了注册Service,代码如下:

1 package com.example.remoteservice

2

3 import com.example.remoteservice.IPet.Stub

4

5 import java.util.ArrayList

6 import java.util.HashMap

7 import java.util.List

8 import java.util.Map

9

10 import android.app.Service

11 import android.content.Intent

12 import android.os.IBinder

13 import android.os.RemoteException

14 import android.util.Log

15

16 public class RemoteService extends Service {

17

18 private PetBinder petBinder

19

20 private static Map>pets = new HashMap>()

21 static {

22 ArrayList list1 = new ArrayList()

23 list1.add(new Pet(candy, 2.2f))

24 list1.add(new Pet(sandy, 4.2f))

25 pets.put(new Person(1, sun, sun), list1)

26

27 ArrayList list2 = new ArrayList()

28 list2.add(new Pet(moon, 5.2f))

29 list2.add(new Pet(hony, 6.2f))

30 pets.put(new Person(1, csx, csx), list2)

31

32 }

33

34 public class PetBinder extends Stub {// 继承IPet接口中的Stub类,Stub类继承了Binder类,所有PetBinder也间接的继承了Binder类

35

36 @Override

37 public List getPets(Person owner) throws RemoteException {

38

39 return pets.get(owner)

40 }

41

42 }

43

44 @Override

45 public IBinder onBind(Intent intent) {

46

47 Log.i(csx, onBind)

48 return petBinder

49 }

50

51 @Override

52 public void onCreate() {

53

54 super.onCreate()

55 Log.i(csx, onCreate)

56 petBinder = new PetBinder()// 实例化Binder

57

58 }

59

60 @Override

61 public boolean onUnbind(Intent intent) {

62

63 Log.i(csx, onUnbind)

64 return super.onUnbind(intent)

65 }

66

67 @Override

68 public void onDestroy() {

69

70 super.onDestroy()

71 Log.i(csx, onDestroy)

72 }

73

74 }

第二步:部署客户端:

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

data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20150108/20150108092529213.png

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

data-cke-saved-src=http://www.2cto.com/uploadfile/Collfiles/20150108/20150108092531218.png

复制代码

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 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 adapter = new ArrayAdapter(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 }

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

android中activity中向service传递参数,有如下方法:

1.在Activity里注册一个BroadcastReceiver,Service完成某个任务就可以发一个广播,接收器收到广播后通知activity做相应的 *** 作。

2.使用bindService来关联Service和Application,应用.apk里的所有组件一般情况都运行在同一个进程中,所以不需要用到IPC,bindService成功后,Service的Client可以得到Service返回的一个iBinder引用,具体的参见Service的文档及onBind的例子,这样Service的引用就可以通过返回的iBinder对象得到,如

public class LocalService extends Service {

// This is the object that receives interactions from clients. See

// RemoteService for a more complete example.

private final IBinder mBinder = new LocalBinder()

public class LocalBinder extends Binder {

LocalService getService() {

return LocalService.this

}

}

@Override

public IBinder onBind(Intent intent) {

return mBinder

}

}

之后Client通过这个iBinder对象得到Service对象引用之后,可以直接和Service通讯,比如读取Service中的值或是调用Service的方法。

在application里面添加handler的set,get方法,在activity里面用handler发送,在service里面实现Handler.Callback接口,在handlemessage里面接收


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9391420.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存