android service(远程service) 知识点
2014-11-13 17:34
141 查看
远程service能够实现多个应用共享一个service,从而实现各个应用之间的通信。
远程service使用的技术是AIDL。创建远程服务步骤:
1. 创建包名,在包下创建一个*.aidl文件,在文件里边定义接口。
注意,定义接口时,不要使用public等关键字。
2. 在服务里边,创建IBander子类对象。
Stub继承Binder类,并实现了MyAIDLService接口。所以,我们只要重写我们自己接口中的方法即可。
3. 在onBind()方法中,返回IBander子类对象。
4. 在AndroidManifest.xml文件中配置服务为远程服务。
通过以上的配置,就能在远程服务所在的应用下使用远程服务了。但,在另一个应用里边如何使用这个远程服务呢?
具体步骤如下:
1. 将*.aidl文件和所在的包,一起包括到自己应用src目录下。
2. 创建ServiceConnection子类对象。
3. 通过远程服务提供的action就可以bindService。
远程service使用的技术是AIDL。创建远程服务步骤:
1. 创建包名,在包下创建一个*.aidl文件,在文件里边定义接口。
package com.example.servicetest.services; interface MyAIDLService { int plus(int a, int b); String toUpperCase(String str); }
注意,定义接口时,不要使用public等关键字。
2. 在服务里边,创建IBander子类对象。
private MyAIDLService.Stub mBinder = new Stub() { @Override public String toUpperCase(String str) throws RemoteException { // TODO Auto-generated method stub return str.toUpperCase(); } @Override public int plus(int a, int b) throws RemoteException { // TODO Auto-generated method stub return a + b; } };
Stub继承Binder类,并实现了MyAIDLService接口。所以,我们只要重写我们自己接口中的方法即可。
3. 在onBind()方法中,返回IBander子类对象。
@Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mBinder; }
4. 在AndroidManifest.xml文件中配置服务为远程服务。
<service android:name="com.example.servicetest.services.MyService" android:process=":remote" > <intent-filter > <action android:name="com.example.servicetest.services.MyService.hahaha"/> </intent-filter> </service>配置远程服务时,一定要在名称前边添加":",所以配置成:remote。不然会出现“ INSTALL_PARSE_FAILED_MANIFEST_MALFORMED”部署异常。
通过以上的配置,就能在远程服务所在的应用下使用远程服务了。但,在另一个应用里边如何使用这个远程服务呢?
具体步骤如下:
1. 将*.aidl文件和所在的包,一起包括到自己应用src目录下。
2. 创建ServiceConnection子类对象。
private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub // 和远程服务连接成功以后,就会调用次方法 try { MyAIDLService binder = MyAIDLService.Stub.asInterface(service); int result = binder.plus(1, 2); String str = binder.toUpperCase("hello world"); System.out.println("result=" + result); System.out.println("str=" + str); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } };通过方法MyAIDLService.Stub.asInterface(service)就能拿到接口对象。从而就能够调用服务里边的方法了。
3. 通过远程服务提供的action就可以bindService。
相关文章推荐
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- 一个简单的demo学习Android远程Service(AIDL的使用)
- 一个简单的demo学习Android远程Service(AIDL的使用)
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service AIDL 远程调用服务之简单音乐播放实例
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android开发学习笔记:Service的远程调用
- Android远程service aidl的用法
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务