安卓aidl简单解析与使用

aidl概述

  • aidl(android interface definition language)是安卓接口定义语言,它可以用于单个service与多个程序之间进行跨进程通讯,从而实现多个应用使用单个service,安卓系统开发中较常见

aidl支持数据类型

  1. java基本数据类型(重点)
  2. List和Map(重点)
    1. 元素必须是aidl支持的数据类型
    2. Server 端具体的类里则必须是 ArrayList 或者 HashMap
  3. 其它aidl生成的接口(了解)
  4. 实现Parcelable的接口(了解)

aidl使用讲解

服务端

  • 创建属于自己的aidl文件,建议另外创建文件夹管理,避免与java文件在同一目录下

package aidl;
interface MyAIDLService {
    String getString();
}
  • 创建完成buil一下,看一下bin文件下是否生成aidl文件,如果生成继续如下操作
  • 创建服务Service,如下,创建MyService
package com.aiden.service_s;

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

import aidl.MyAIDLService;

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

     @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("TAG", "Aiden-" + "onDestroy");
    }

     @Override
    public IBinder onBind(Intent intent) {
    	Log.v("TAG", "Aiden-" + "Mybind");
        return new Mybind();
    }

    class Mybind extends MyAIDLService.Stub {
        @Override
        public String getString() throws RemoteException {
            String string = "我是从服务起返回的";
            return string;
        }
    }
}

注:远程服务(RemoteService)与本地服务(LocalService)区别,本地服务的时候内部类继承的是Binder,而现在继承的是MyAIDLService.Stub(自己定义的aidl接口)

  • 启动该服务
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
bindService(intent, mConnection, BIND_AUTO_CREATE);

private ServiceConnection mConnection = new ServiceConnection() {
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
	}

};
  • 服务端的AndroidManifest.xml

     
          
     
 

 

客户端

  • 将刚刚在服务端创建的MyAIDLService原封不动的复制到客户端来。(注意:路径要一模一样)
  • 绑定服务
Intent intent = new Intent();
intent.setAction("com.aiden.service_s.MyService");
//从 Android 5.0开始 隐式Intent绑定服务的方式已不能使用,所以这里需要设置Service所在服务端的包名
intent.setPackage("com.aiden.service_s");
bindService(intent, mConnection, BIND_AUTO_CREATE);
private ServiceConnection mConnection = new ServiceConnection() {
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		Log.v(Tag, "Aiden-" + "stopService");
		myAIDLService = MyAIDLService.Stub.asInterface(service);
		try {
            String str = myAIDLService.getString();
				Log.v(Tag, "Aiden-str-----" + str);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
    @Override
	public void onServiceDisconnected(ComponentName name) {
		Log.v(Tag, "Aiden-" + "stopService");
		myAIDLService = null;
	    }

    };

至此可以看见打印:我是从服务起返回的

  • 解除绑定
unbindService(mConnection);

 

aidl总结

  • 优点:通讯速度快,支持实时通信,支持一对多并发通信,支持远程过程调用,系统底层直接共享内存,性能稳定,效率高

  • 缺点:使用复杂,需要创建aidl文件,需要处理好线程同步问题

注释:学识甚浅,大家仅作参考

你可能感兴趣的:(安卓aidl简单解析与使用)