android的进程间通信aidl

参考:http://blog.csdn.net/hitlion2008/article/details/9824009

受限于Android本身对AIDL的设计,利用AIDL传输的数据大小不得大于1MB,否则会报TransactionTooLargeException,目前代码里暂时没有考虑该问题的解决方案。

aidl分层结构:












aidl只能传基本数据类型,其他类型可以通过实现parcelable。

Java中的八种基本数据类型,包括 byte,short,int,long,float,double,boolean,char。
String 类型。
CharSequence类型。
List类型:List中的所有元素必须是AIDL支持的类型之一,或者是一个其他AIDL生成的接口,或者是定义的parcelable(下文关于这个会有详解)。List可以使用泛型。
Map类型:Map中的所有元素必须是AIDL支持的类型之一,或者是一个其他AIDL生成的接口,或者是定义的parcelable。Map是不支持泛型的。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中的AIDLAndroid Interface Definition Language)是一种用于进程间通信的机制,它允许在不同进程中的组件之间进行通信。AIDL是一个基于接口的编程语言,它定义了一组方法,这些方法可以被其他进程中的组件调用。 AIDL的使用步骤如下: 1.定义AIDL接口:定义接口和方法,并在方法中指定参数和返回值类型。 2.实现AIDL接口:实现AIDL接口中定义的方法。 3.注册AIDL服务:在AndroidManifest.xml文件中注册服务。 4.使用AIDL服务:获取AIDL对象并调用方法。 下面是一个简单的例子,演示如何使用AIDL进行进程间通信。 1.定义AIDL接口 ``` interface IMyAidlInterface { void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); } ``` 2.实现AIDL接口 ``` public class MyAidlService extends Service { private static final String TAG = "MyAidlService"; private IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { Log.d(TAG, "basicTypes: " + anInt + ", " + aLong + ", " + aBoolean + ", " + aFloat + ", " + aDouble + ", " + aString); } }; @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } } ``` 3.注册AIDL服务 在AndroidManifest.xml文件中添加以下代码: ``` <service android:name=".MyAidlService" android:exported="true"> <intent-filter> <action android:name="com.example.MyAidlService" /> </intent-filter> </service> ``` 4.使用AIDL服务 ``` public class MainActivity extends AppCompatActivity { private IMyAidlInterface mService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setAction("com.example.MyAidlService"); intent.setPackage("com.example"); bindService(intent, mConnection, BIND_AUTO_CREATE); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mService = IMyAidlInterface.Stub.asInterface(iBinder); try { mService.basicTypes(1, 2L, true, 3.0f, 4.0, "Hello, AIDL!"); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { mService = null; } }; } ``` 在上面的代码中,我们首先创建一个Intent对象,指定要绑定的服务的包名和类名。然后调用bindService()方法绑定服务,并在onServiceConnected()方法中获取AIDL对象,调用basicTypes()方法向服务传递参数。最后,在onServiceDisconnected()方法中释放AIDL对象。 以上就是使用AIDL进行进程间通信的基本步骤。需要注意的是,在使用AIDL时,必须确保服务已经启动,并且在AndroidManifest.xml文件中注册了服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值