位置: 编程技术 - 正文
推荐整理分享AIDL 进程间通信(进程间的通信方),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:进程间通信例子,进程间通信pipe,进程间通信例子,aidl进程间通信原理,进程间如何通信,进程间如何通信,进程间通信 ipc,进程间通信 ipc,内容如对您有帮助,希望把文章链接给更多的朋友!
1.新建IRemoteService.aidl
view sourceprint?1.packagecom.tang.remoteservicedemo;2.interfaceIRemoteService {3.String getInfo();4.}从内容中也可以看出,这东西类一个接口。既然定义了这么一个玩意,那么我们就要去实现它。
2.新建IService“实现”IRemoteService“接口”
view sourceprint?.packagecom.tang.remoteservicedemo;. .importjava.util.Date;. .importandroid.app.Service;.importandroid.content.Intent;.importandroid.os.IBinder;.importandroid.os.RemoteException;. .publicclass IService extendsService.{.privateIBinder iBinder = new IRemoteService.Stub() {.@Override.publicString getInfo() throws RemoteException {.// TODO Auto-generated method stub.returnnew Date(System.currentTimeMillis()).toLocaleString()" 来自远程服务的信息!";.}.};.@Override.publicIBinder onBind(Intent intent) {.// TODO Auto-generated method stub.returniBinder;.}.}基于Binder的不同进程间通信,Client与Service在不同的进程中,对用户程序而言当调用Service返回的IBinder接口后,访问Service中的方法就如同调用自己的函数一样。
3.配置IService供其他进程调用
view sourceprint?1.<service android:name="com.tang.remoteservicedemo.IService"2.android:process=":remote">3.<intent-filter>4.<action android:name="com.tang.remoteservicedemo.IService"/>5.</intent-filter>6.</service>配置一个所属进程名和一个action。
通过前面三个步骤,这个含有getInfo()方法的service就可以给别人调用了,下面在客户端调用它
4.新建一个ClientDemo工程将含有IRemoteService.aidl的那个包全部拷贝到src下,只留下aidl文件,其他全删除。
5.新建MainActivity,其他就是绑定service的操作了
view sourceprint?.packagecom.tang.clientdemo;. .importjava.util.Timer;.importjava.util.TimerTask;. .importcom.tang.remoteservicedemo.IRemoteService;. .importandroid.app.Activity;.importandroid.content.ComponentName;.importandroid.content.Context;.importandroid.content.Intent;.importandroid.content.ServiceConnection;.importandroid.os.Bundle;.importandroid.os.IBinder;.importandroid.os.RemoteException;.importandroid.util.Log;. . .publicclass MainActivity extendsActivity {. .privateIRemoteService iService = null;.privateboolean isBinded =false;. .ServiceConnection conn =new ServiceConnection() {. .@Override.publicvoid onServiceDisconnected(ComponentName name) {.// TODO Auto-generated method stub.isBinded =false;.iService =null;.}. .@Override.publicvoid onServiceConnected(ComponentName name, IBinder service) {.// TODO Auto-generated method stub.iService = IRemoteService.Stub.asInterface(service);.isBinded =true;.}.};. .publicvoid doBind() .{.Intent intent =new Intent("com.tang.remoteservicedemo.IService");.bindService(intent, conn, Context.BIND_AUTO_CREATE);.}. .publicvoid doUnbind() .{.if(isBinded) .{.unbindService(conn);.iService =null;.isBinded =false;.}.}.@Override.protectedvoid onCreate(Bundle savedInstanceState) {.super.onCreate(savedInstanceState);.setContentView(R.layout.activity_main);.newThread(new Runnable() { .@Override.publicvoid run() {.// TODO Auto-generated method stub.doBind();.}.}).start();.Timer timer =new Timer();.timer.schedule(task,0, );. .}.TimerTask task =new TimerTask() {.@Override.publicvoid run() {.// TODO Auto-generated method stub.if(iService!=null).{.try{.Log.i("AAA", iService.getInfo());.}catch (RemoteException e) {.// TODO Auto-generated catch block.e.printStackTrace();.}.}.else.{.Log.i("AAA","iService!=null");.}. .}.};. .@Override.protectedvoid onDestroy() {.// TODO Auto-generated method stub.doUnbind();.task.cancel();.super.onDestroy();.}.}执行之后的Log如下:
为什么会有一个为空的Log呢,因为绑定也是要时间的嘛....
Android Studio --自动删除没有用的资源,给APK减减肥 有时候我们添加的一些资源,如图片和一些没用的代码,以及在添加第三方库的时候我们只需要使用其中的一部分功能和一部分资源,那么这个时候如
Android 圆形/圆角图片的方法 Android圆形/圆角图片的方法目前网上有很多圆角图片的实例,Github上也有一些成熟的项目。之前做项目,为了稳定高效都是选用Github上的项目直接用。但
CentOS使用android 一,Linux部署JDK1.1下载JDK地址:
标签: 进程间的通信方
本文链接地址:https://www.jiuchutong.com/biancheng/385096.html 转载请保留说明!友情链接: 武汉网站建设