AIDL
来源:互联网 发布:无线办公鼠标推荐知乎 编辑:程序博客网 时间:2024/06/03 08:32
icp:interprocess communication :
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是一个异步方法。
- AIDL
- aidl
- AIDL
- AIDL
- AIDL
- AIDL
- AIDL
- aidl
- aidl
- AIDL
- AIDL
- aidl
- AIDL
- AIDL
- AIDL
- aidl
- aidl
- AIDL
- class org.apache.commons.dbcp.basicdatasource not found解决办法
- mysql日期加减
- jQuery源码解析(5)—— Animation动画
- sublist()的用法
- openwrt之 查看连接的客户端之 arp
- AIDL
- 怎样导出ZBrush中的模型和贴图
- iOS项目结构目录参考图示
- Android API Guides---System Permissions
- BIND配置文件详解
- 蓝桥杯—— 算法训练 Anagrams问题
- python psutil跨平台监控系统或进程信息(CPU,mem)
- iOS开发中,在label中改变数字颜色的方法(改变某个关键词的方法)
- 控制符号的可见性