ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

AIDL进程间通信

2021-07-10 11:55:19  阅读:199  来源: 互联网

标签:CameraControler return AIDL 间通信 Override boolean 进程 android public


一. 服务端程序

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有