Android中如何利用AIDL机制调用远程服务

所属分类: 软件编程 / Android 阅读数: 69
收藏 0 赞 0 分享

在Android中,每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢?显然, Java中是不支持跨进程内存共享的。因此要传递对象,需要把对象解析成操作系统能够理解的数据格式,以达到跨界对象访问的目的。在JavaEE中,采用RMI通过序列化传递对象。在Android中,则采用AIDL(Android Interface DefinitionLanguage:接口描述语言)方式实现。

AIDL是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现Android设备上的两个进程间通信(IPC)。AIDL的IPC机制和EJB所采用的CORBA很类似,进程之间的通信信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。

服务端:

//CalculateInterface.aidl
package com.itheima.aidl.calculate;
interface CalculateInterface {
double doCalculate(double a, double b);
}
//CalculateService.java
package com.itheima.myaidl.server;
import com.itheima.aidl.calculate.CalculateInterface;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class CalculateService extends Service{
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {
@Override
public double doCalculate(double a, double b) throws RemoteException {
return a+b;
}
};
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind...");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("test","onUnbind...");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.i("test","onCreate...");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("test","onDestroy...");
}
}
//服务端manifast文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.myaidl.server"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity 
android:name="com.itheima.myaidl.server.MainActivity"
android:configChanges="locale|layoutDirection"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.myaidl.server.CalculateService">
<intent-filter>
<action android:name="com.itheima.myaidl.server.CalculateService" />
</intent-filter>
</service>
</application>
</manifest>
//客户端
//MainActivity.java
package com.itheima.myaidl.client;
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.os.RemoteException;
import android.util.Log;
import com.itheima.aidl.calculate.CalculateInterface;
public class MainActivity extends Activity {
private CalculateInterface mService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","service disconnected...");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","service connected...");
mService = CalculateInterface.Stub.asInterface(service); //获取接口实例
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定远程服务
Bundle bundle = new Bundle();
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setAction("com.itheima.myaidl.server.CalculateService");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
//TODO activity加载完毕时回调此方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
try{
double result = mService.doCalculate(1, 2);
Log.i("test","result===>"+result);
}catch(RemoteException e){
e.printStackTrace();
}
}
super.onWindowFocusChanged(hasFocus);
}
@Override
protected void onDestroy() {
unbindService(mServiceConnection); //解绑远程服务
super.onDestroy();
}
}

运行结果截图:

以上所述是小编给大家介绍的Android中如何利用AIDL机制调用远程服务的相关知识,希望对大家有所帮助!

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多