系统相关
首页 > 系统相关> > AIDL进程间通信

AIDL进程间通信

作者:互联网

一. 服务端程序

1. 新建一个单例的类, 如CameraControler.java, 实现进程内通信相关的接口,内容如下:

package com.android.camera;

public class CameraControler {
	private OnControlListener mListener = null;
	private static CameraControler sInstance = null;

	public static interface OnControlListener {
		boolean takepicture();
		boolean recorder(boolean state);
		void factoryreset();
	}

	private CameraControler() {
	}

	public final static CameraControler getInstance() {
		if (sInstance == null) {
			synchronized (CameraControler.class) {
				if (sInstance == null) {
					CameraControler.sInstance = new CameraControler();
				}
			}
		}
		return sInstance;
	}

	public CameraControler setOnControlListener(OnControlListener listener) {
		mListener = listener;
		return this;
	}
	
	public boolean takepicture() {
		if(mListener==null)
			return false;
		return mListener.takepicture();
	}
	
	public boolean recorder(boolean state) {
		if(mListener==null)
			return false;
		return mListener.recorder(state);
	}
	
	public boolean factoryreset() {
		if(mListener==null)
			return false;
		mListener.factoryreset();
		return true;
	}
}

2. 在要被控制的类中添加成员变量, 并实现相关接口:

private CameraControler mCameraControler = CameraControler.getInstance();

mCameraControler.setOnControlListener(new CameraControler.OnControlListener() {
	@Override
	public boolean takepicture() {
		return mCameraAppUi.getPhotoShutter().performClick();
	}

	@Override
	public boolean recorder(boolean state) {
		if(state) {
			if(!isVideoMode())
				return mCameraAppUi.getVideoShutter().performClick();
		} else{
			if(isVideoMode())
				return mCameraAppUi.getVideoShutter().performClick();
		}
		return false;
	}

	@Override
	public void factoryreset() {
		sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
	}
});

3. 写一个服务,如CameraServer.java,声明单例对象,并实现相关接口,通过调用单例对象的对应接口达到进程类通信的功能

package com.android.camera;

import com.llx.lib.Debug;

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

public class CameraServer extends Service {

	protected CameraControler mCameraControler = CameraControler.getInstance();
	
	@Override
	public IBinder onBind(Intent intent) {
		return new ICameraServer.Stub() {		
			@Override
			public boolean takepicture() throws RemoteException {
				return mCameraControler.takepicture();
			}
			
			@Override
			public boolean recorder(boolean state) throws RemoteException {
				return mCameraControler.recorder(state);
			}
			
			@Override
			public void factoryreset() throws RemoteException {
				mCameraControler.factoryreset();
			}

			@Override
			public void printf() throws RemoteException {
				Debug.printf("QQ: 515311445", null);
			}
		};
	}
}

4. 在AndroidManifest.xml中添加服务的声明

        <service
            android:name="com.android.camera.CameraServer"
            android:enabled="true"
            android:label="@string/app_name" >  
            <intent-filter>
                <intent-filter>  
                	<action android:name="com.android.camera.ICameraServer" />
                	<category android:name="android.intent.category.DEFAULT" />
            	</intent-filter>
            </intent-filter>
        </service>

5. 新建aidl文件,如ICameraServer.aidl,其中添加服务中提供的相关接口,例如:

package com.android.camera;
interface ICameraServer {
		boolean takepicture();
		boolean recorder(boolean state);
		void factoryreset();
		void printf();
}

6. 如果要放到系统源码中编码, 需要在Android.mk中添加引用,如下:

LOCAL_SRC_FILES += src/com/android/camera/ICameraServer.aidl

二. 客户端程序

1. 把aidl文件拷贝到客户端程序中, 注意: aidl文件包名要与服务端包名一致; 并在要调用aidl接口的文件中声明ICameraServer camera;

2. 创建ServiceConnection对象, 如下:

private ServiceConnection CameraConnection = new ServiceConnection() {
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		camera = ICameraServer.Stub.asInterface(service);
		try {
			camera.printf();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Toast.makeText(TestAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show();
	}
		
	@Override
	public void onServiceDisconnected(ComponentName name) {
	}
};

3. 初始化时调用如下代码:

Intent intent = new Intent();
intent.setClassName("com.android.gallery3d", "com.android.camera.CameraServer");
bindService(intent, CameraConnection, Context.BIND_AUTO_CREATE);

4. Destroy时调用如下代码:

unbindService(CameraConnection);
CameraConnection = null;

 

标签:CameraControler,return,AIDL,间通信,Override,boolean,进程,android,public
来源: https://blog.51cto.com/u_15298588/3034191