Android studio aidl 总结(二)
来源:互联网 发布:朝鲜核讹诈中国知乎 编辑:程序博客网 时间:2024/05/18 14:14
icp是interprocess communication的缩写其实就是内部进程通信.
前面说了aidl的使用 这边是链接:aidl使用
----大家都知道我们可以用aidl可以跨进程访问其他的应用程序,和其他的应用程序通许.但是android中有很对技术都可以实现跨进程的访问(例如广播,ContentProvider.Messager)还比它方便.....于是问题来:为什么android会有aidl,它和其他的方式有什么不同呢?----现在我们就来探究下:
首先
广播:在anroid我们send一个Broadcast给BroadcastReceiver,但是BroadcastReceiver是不能返回信息或者传递信息在给发送者的所以是一种单向传递.
ContentProvider:用于提供数据和存储数据的方法,就可以向其他应用共享其数据.
------这俩中其实算不上跨进程的通信.
Messager:俩个特点 一个是线程安全一个是单线程.
而aidl恰恰和他相反支持多线程并发操作,同时则会带来线程的不安全.不过我们大部分情况是不会用到这个.
明白了什么时候使用aidl的话,我们现在就来看看aidl具体上层的实现流程.
第一步我们学入我们的forActivity.aidl的文件放入Android studio 并生成 对应的android 如果不会请点击上面的aidl使用
forActivity.aidl:
package com.xu.test.aidl;interface forActivity{ void performAction();}放入了这个aidl文件我们就看看下面自动生成的forActivity.java的代码:
/* * This file is auto-generated. DO NOT MODIFY. * Original file: /home/xuguang/file/workspace_studio/Test/src/main/aidl/com/xu/test/aidl/forActivity.aidl */package com.xu.test.aidl;public interface forActivity extends android.os.IInterface { /** * Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.xu.test.aidl.forActivity { private static final java.lang.String DESCRIPTOR = "com.xu.test.aidl.forActivity"; /** * Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.xu.test.aidl.forActivity interface, * generating a proxy if needed. */ public static com.xu.test.aidl.forActivity asInterface(android.os.IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof com.xu.test.aidl.forActivity))) { return ((com.xu.test.aidl.forActivity) iin); } return new com.xu.test.aidl.forActivity.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_performAction: { data.enforceInterface(DESCRIPTOR); this.performAction(); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.xu.test.aidl.forActivity { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public void performAction() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_performAction, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } } static final int TRANSACTION_performAction = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } public void performAction() throws android.os.RemoteException;}
可以看出系统生成的这个.java的文件里 创建了一个 和我们文件名的相同的接口且该接口继承IInterface接口
这里我们看下IInterface其实就是一个简单的接口
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.os;/** * Base class for Binder interfaces. When defining a new interface, * you must derive it from IInterface. */public interface IInterface{ /** * Retrieve the Binder object associated with this interface. * You must use this instead of a plain cast, so that proxy objects * can return the correct result. */ public IBinder asBinder();}
可以看出他只有个抽象方法就 asBinder();
接下来就可以看到定义了一个内部静态类 名字叫做Stub 他继承了Binder 实现了我们这个java本身的接口forActivity
我们可以直接看到这个方法
/** * Cast an IBinder object into an com.xu.test.aidl.forActivity interface, * generating a proxy if needed. */ public static com.xu.test.aidl.forActivity asInterface(android.os.IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof com.xu.test.aidl.forActivity))) { return ((com.xu.test.aidl.forActivity) iin); } return new com.xu.test.aidl.forActivity.Stub.Proxy(obj); }
如果我们在一个Activity要使用这个aidl的时候我们需要做的是 绑定一个远程的服务
bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
而mConnection是我们在使用一个远程服务所需要的一个回调接口.
<pre name="code" class="java"> forService mService ; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log("onConnection Service"); mService=forService.Stub.asInterface(iBinder); try { mService.registerCall(mCallback); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { Log("disconnection"); mService=null; } };
而在绑定Server时 Server 绑定成功则回调onServiceConnected 解除绑定则回调onServiceDisconnected
在onServiceConnected时 会回调了一个IBinder 接口对象 ,这个 对象是我们 forActiviyt 对象的Binder 于是在里面我们就会要实现Stu静态类的方法 也就是 aidl 里的
public void performAction() :
而这个IBinder 就是用来Activity --- Service 之间的通信
还记的上面 asInterface方法 于是我们可以吧这个IBinder对象传进入他会使用本地代理返回一个IBinder对象
这个本地代理就就是在我们静态的Stu类里在申明一个内部类的静态.
private static class Proxy implements com.xu.test.aidl.forActivity { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public void performAction() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_performAction, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } }
从而我们这个 IBinder对象 即可以达Activity和aidl 直接通信 . IBinder 对象可以调用performAction() :
而该方法已经在 Service 中实现了. 这样我们就形成了aidl的通信.
我自己都不知道在些什么 自己想这自己的语言该如何组织
- Android studio aidl 总结(二)
- Android studio 使用aidl总结(一)
- Android studio 上使用aidl总结
- Android studio import aidl
- Android Studio 使用AIDL
- Android Studio导入aidl
- [Android Studio] AIDL
- android studio AIDL使用方法。
- Android studio AIDL详解
- AIDL导入Android Studio
- Android Studio 使用AIDL
- Android Studio AIDL
- Android Studio新建AIDL
- Android Studio 使用AIDL
- Android studio中的aidl
- android studio 创建aidl
- Android Studio AIDL实现
- Android studio 创建AIDL
- Android studio 使用aidl总结(一)
- 关于用UDP网络游戏服务器的一些探讨
- Linux /dev目录详解
- 视频编解码链接
- 【九章算法】北美求职之面试题目
- Android studio aidl 总结(二)
- Flighting游戏服务器:关于TCP和UDP
- strstr模板
- VC调试技巧
- php 获取YouTube视频信息
- Apache 多域名多站点设置
- 【新手】Ubuntu下虚拟机屏幕太小
- Eclipse 的一些调试技巧
- hdu 3172 Virtual Friends 并查集