AIDL
2016-04-11 13:45
591 查看
1、什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android进程间通信接口的描述语言,通过它我们可以定义进程间的通信接口
icp:interprocess communication :进程间通信
2、使用场景:
进程间通信,一个进程为其他多个进程提供服务。
实验项目包结构:
服务器:
客户端:
A:服务器端(安卓项目):
一、定义接口(.aidl文件,自动编译成.java文件):
package com.yangxiaoru.test_aidl;
interface Minterface{
int plus(in int a,in int b) ;
}
2、暴露接口:
public class MyServer extends Service{
@Override
public IBinder onBind(Intent arg0) {
// 暴露接口
return new Minterface.Stub() {
@Override
public int plus(int a, int b) throws RemoteException {
return a+b;
}
};
}
}
3、在XML中申明
<service android:name="com.yangxiaoru.test_aidl.MyServer" >
<intent-filter>
<action android:name="yangxiaoru" />
</intent-filter>
</service>
二、客户端:
1、在于服务器同名的包下,建立同名的.aidl文件
2、通过BindService使用服务:
public class MainActivity extends Activity {
private Minterface mInterface;
private ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inite();
toBindService();
}
private void toBindService() {
Intent intent = new Intent("yangxiaoru");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private void inite() {
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
mInterface = null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
mInterface = Minterface.Stub.asInterface(binder);
try {
int x = mInterface.plus(2, 6);
System.out.println("2+6==" + x);
} catch (RemoteException e) {
System.out.println("远程调用出错");
}
}
};
}
}
注:bindService是一个异步方法。
icp:interprocess communication :进程间通信
2、使用场景:
进程间通信,一个进程为其他多个进程提供服务。
实验项目包结构:
服务器:
客户端:
A:服务器端(安卓项目):
一、定义接口(.aidl文件,自动编译成.java文件):
package com.yangxiaoru.test_aidl;
interface Minterface{
int plus(in int a,in int b) ;
}
2、暴露接口:
public class MyServer extends Service{
@Override
public IBinder onBind(Intent arg0) {
// 暴露接口
return new Minterface.Stub() {
@Override
public int plus(int a, int b) throws RemoteException {
return a+b;
}
};
}
}
3、在XML中申明
<service android:name="com.yangxiaoru.test_aidl.MyServer" >
<intent-filter>
<action android:name="yangxiaoru" />
</intent-filter>
</service>
二、客户端:
1、在于服务器同名的包下,建立同名的.aidl文件
2、通过BindService使用服务:
public class MainActivity extends Activity {
private Minterface mInterface;
private ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inite();
toBindService();
}
private void toBindService() {
Intent intent = new Intent("yangxiaoru");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private void inite() {
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
mInterface = null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
mInterface = Minterface.Stub.asInterface(binder);
try {
int x = mInterface.plus(2, 6);
System.out.println("2+6==" + x);
} catch (RemoteException e) {
System.out.println("远程调用出错");
}
}
};
}
}
注:bindService是一个异步方法。
相关文章推荐
- 下一代机器学习-在浏览器中训练深度学习模型Next Generation Machine Learning - Training Deep Learning Models in a Browser
- trail particle制作血管
- leetcode 11 Container With Most Water
- Argument list too long: recursive header expansion failed at
- RAID知识讲解
- LeetCode 11. Container With Most Water
- LeetCode(43)-Contains Duplicate II
- LeetCode(43)-Contains Duplicate II
- LeetCode(43)-Contains Duplicate II
- 报错:failed to get the task for process XXX(解决方案)
- Domain generalization 简介
- BaiduMap---百度地图官方Demo之调用百度地图(介绍如何调启百度地图实现自身业务功能)
- Command /usr/bin/codesign failed with exit code 1
- jenkins排错---Waiting for next available executor
- 使用anddroid studio编译报错: Error:Execution failed for task ':mergeDebugResources'.
- 关于人工智能和人类未来的吐槽
- git:Please enter a commit message to explain why this merge is necessary
- 分类模型的性能评估——以SAS Logistic回归为例(3): Lift和Gain
- 解决Failed to parse SourceMap: http:xxx 问题
- 6.8.1 qmgr_active_drain函数:从active队列发信