Android AIDL

一、AIDL介绍

aidl Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口

Aidl与其他通讯的区别: “只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理。

二、AIDL使用

 

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class AIDLRemoteService extends Service {
    private static final String TAG = "AIDLRemoteService";

    private final myAIDL.Stub mBinder=new myAIDL.Stub(){
        @Override
        public void testMethod() throws RemoteException {
            Log.d(TAG, "testMethod: "+"this is myAIDLTest");
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

// myAIDL.aidl
package com.example.aidltest;

// Declare any non-default types here with import statements

interface myAIDL{
	void testMethod();
}

package com.example.aidltest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private Button btnBindService;
    private Button btnStartMethod;

    private myAIDL mMyAIDL;

    private ServiceConnection mServiceConnection = new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            Log.e(TAG, "onServiceConnected");
            mMyAIDL = myAIDL.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            Log.e(TAG, "onServiceDisconnected");
            mMyAIDL = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        btnBindService =(Button) findViewById(R.id.btn_bind_service);
        btnStartMethod=(Button)findViewById(R.id.btn_start_method);
        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AIDLRemoteService.class);
                bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
            }
        });
        btnStartMethod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMyAIDL!=null){
                    try {
                        mMyAIDL.testMethod();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }else{
                    Toast.makeText(MainActivity.this, "服务被异常杀死,请重新开启。", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
    }
}

里面有四个个重要的文件,一个是myAIDL.aidl文件,一个是AIDLRemoteService.java文件,一个是MainActivity文件,最后一个是myAIDL.Java文件。前面三个文件不做多的分析,主要分析myAIDL.java文件。

 

三、AIDL.java文件


从其中我们可以看出AIDL.java文件实际上是对Binder进行了代理模式处理的,重写了Binder里面的onTransact()方法,Proxy负责给远端Service发送信息,Stub负责重写接口方法、接收客户端的信息、进行调用方法


/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\workplace2\\AIDLTest\\src\\com\\example\\aidltest\\myAIDL.aidl
 */
package com.example.aidltest;

// Declare any non-default types here with import statements

public interface myAIDL extends android.os.IInterface {
	/** Local-side IPC implementation stub class. */
	public static abstract class Stub extends android.os.Binder implements
			com.example.aidltest.myAIDL {
		private static final java.lang.String DESCRIPTOR = "com.example.aidltest.myAIDL";

		/** Construct the stub at attach it to the interface. */
		public Stub() {
			this.attachInterface(this, DESCRIPTOR);
		}

		/**
		 * Cast an IBinder object into an com.example.aidltest.myAIDL interface,
		 * generating a proxy if needed.
		 */
		public static com.example.aidltest.myAIDL asInterface(
				android.os.IBinder obj) {
			if ((obj == null)) {
				return null;
			}
			android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
			if (((iin != null) && (iin instanceof com.example.aidltest.myAIDL))) {
				return ((com.example.aidltest.myAIDL) iin);
			}
			return new com.example.aidltest.myAIDL.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_testMethod: {
				data.enforceInterface(DESCRIPTOR);
				this.testMethod();
				reply.writeNoException();
				return true;
			}
			}
			return super.onTransact(code, data, reply, flags);
		}

		private static class Proxy implements com.example.aidltest.myAIDL {
			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 testMethod() 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_testMethod, _data,
							_reply, 0);
					_reply.readException();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
			}
		}

		static final int TRANSACTION_testMethod = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
	}

	public void testMethod() throws android.os.RemoteException;
}



### Android AIDL 使用教程 #### 定义 Parcelable 对象 为了使自定义数据类型能够在不同进程中传递,需要创建实现了 `Parcelable` 接口的对象,并通过 AIDL 文件描述这些对象。这允许其他组件能够识别并处理该类实例。 ```java // MyData.aidl package com.example; parcelable MyCustomType; ``` 此操作确保了即使是在不同的应用程序之间也能安全有效地传输复杂的数据结构[^1]。 #### 创建接口声明文件 AIDL 的核心用途之一就是用于定义远程服务的方法签名。开发者需编写 `.aidl` 文件来指定哪些功能可供外部调用者访问: ```aidl // IMyService.aidl package com.example.service; interface IMyService { void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); } ``` 上述代码片段展示了如何声明一个简单的远程过程调用(RPC),其中包含了多种基本参数类型的函数原型。 #### 实现 AIDL 接口的服务端逻辑 一旦完成了 AIDL 文件的设计工作,则可以在服务器端具体实现相应的业务流程。通常情况下,这涉及到继承由编译器生成的帮助类(如 `IMyService.Stub`),进而提供实际的功能执行路径。 ```java public class MyServiceImpl extends IMyService.Stub { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { // Implementation here... } } ``` 这种设计模式使得客户端无需关心底层通信细节即可轻松调用远端提供的能力。 ### 常见问题解决方案 当遇到无法解析的 AIDL 类型时,应确认对应的 parcelable 描述符已被正确定义且位于正确的包空间下;另外还需注意检查 build.gradle 中是否已配置好必要的依赖项以及源码集设置正确无误。 对于权限相关的问题,比如尝试读取或写入 SD 卡失败的情况,除了要保证应用清单中申请到了合适的存储许可之外,在运行时也得动态请求用户的同意才能继续操作受限资源[^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值