一. 服務端程序
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;