android绑定远程服务以及android接口定义语言(aidl)
2015-12-10 14:40
603 查看
在Andorid平台中,各个组件运行在自己的进程中,他们之间是不能相互访问的,但是在程序之间是不可避免的要传递一些对象,在进程之间相互通信。为了实现进程之间的相互通信,Andorid采用了一种轻量级的实现方式RPC(Remote
Procedure Call 远程进程调用)来完成进程之间的通信,并且Android通过接口定义语言(Andorid
Interface Definition Language ,AIDL)来生成两个进程之间相互访问的代码,例如,你在Activity里的代码需要访问Service中的一个方法,那么就可以通过这种方式来实现了。
AIDL是Android的一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成 AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象。
AIDL RPC机制是通过接口来实现的,类似Windows中的COM或者Corba,但他是轻量级的,客户端和被调用实现之间是通过代理模式实现的,代理类和被代理类实现同一个接口Ibinder接口。
下面是实现Activity访问Service例子的步骤:
远程服务段端:
com.example.remote.Myservice
AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。重要的是必须导入导入除了内建类型(例如:int,boolean等)外的任何其他类型,哪怕是这些类型是在与接口相同的包中。具体的要求如下:
JAVA基本数据类型不需要导入
String,List,Map和CharSequence不需要导入
其次在清单文件中声明服务:
二:编写调用远程服务的程序:
1:将被调用服务的aidl文件copy过来,注意,报名必须一致
调用端布局文件
activity_main.xml
调用实现:
实验结果:
Procedure Call 远程进程调用)来完成进程之间的通信,并且Android通过接口定义语言(Andorid
Interface Definition Language ,AIDL)来生成两个进程之间相互访问的代码,例如,你在Activity里的代码需要访问Service中的一个方法,那么就可以通过这种方式来实现了。
AIDL是Android的一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成 AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象。
AIDL RPC机制是通过接口来实现的,类似Windows中的COM或者Corba,但他是轻量级的,客户端和被调用实现之间是通过代理模式实现的,代理类和被代理类实现同一个接口Ibinder接口。
下面是实现Activity访问Service例子的步骤:
远程服务段端:
com.example.remote.Myservice
package com.example.remote; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; import android.widget.Toast; public class Myservice extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub System.out.println("服务绑定"); return new MiddlePerson(); } @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("服务创建"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("服务结束"); super.onDestroy(); } public void methodInservice(String arr){ System.out.println("我是远程服务方法,我被调用了"); } private class MiddlePerson extends IMiddlePerson.Stub{ @Override public void call(String string) throws RemoteException { // TODO Auto-generated method stub methodInservice(string); } } }编写aidl接口
AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。重要的是必须导入导入除了内建类型(例如:int,boolean等)外的任何其他类型,哪怕是这些类型是在与接口相同的包中。具体的要求如下:
JAVA基本数据类型不需要导入
String,List,Map和CharSequence不需要导入
package com.example.remote; interface IMiddlePerson { void call(String string); }
其次在清单文件中声明服务:
<service android:name="com.example.remote.Myservice"> <intent-filter ><action android:name="com.example.remote.Myservice"/></intent-filter> </service>
二:编写调用远程服务的程序:
1:将被调用服务的aidl文件copy过来,注意,报名必须一致
调用端布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="bind" android:text="绑定远程" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mystop" android:text="解除绑定" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="调用方法" /> </LinearLayout>
调用实现:
package com.example.bindremote; import com.example.remote.IMiddlePerson; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { private IMiddlePerson im; private Myconn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void mystop(View vew){ unbindService(conn); } public void bind(View vew){ Intent service=new Intent(); service.setAction("com.example.remote.Myservice"); conn=new Myconn(); bindService(service, conn, BIND_AUTO_CREATE); } public void call(View vew){ try { im.call("leigewudi"); System.out.println(im); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class Myconn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub im=IMiddlePerson.Stub.asInterface(service); System.out.println("服务被调用了"+im); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub unbindService(conn); System.out.println("服务结束"); } } }
实验结果:
相关文章推荐
- 【android】dp和px转化
- Android 的设置项 II-Reading Settings&Custom Preference
- android布局详解
- Android view 数据缓存
- Android特效Toast详解
- 读《50 Android Hacks》笔记整理Hack 44~Hack 50
- Android开发基本命名规范
- Android_(自定义view_3)
- android如何在一个布局文件中包含另外一个布局文件(include属性)
- Android 内存优化汇总
- 【Android效果集】弹幕效果
- Android学习摘记——简单的自定义View(自绘控件)
- Android自定义组合控件详细示例 (附完整源码)
- android studio 百度地图使用环境配置及230错误码解决方法
- Android获取控件坐标,触摸点坐标 getX getRawX
- Android Studio基础之项目目录结构(四)-学习篇
- Android教程 -09 数据的持久化存储
- Android教程 -09 数据的持久化存储
- Android 开发常用的工具类
- Fragment生命周期 android基础三