标签:开源框架 android平台 数据管理 datadroid 框架
<span style="font-family:Microsoft YaHei;font-size:14px;">public class CameraRequestManager extends RequestManager { // Singleton management private static CameraRequestManager sInstance; public synchronized static CameraRequestManager from(Context context) { if (sInstance == null) { sInstance = new CameraRequestManager(context); } return sInstance; } private CameraRequestManager(Context context) { super(context,CameraRequestService.class); } }</span>2.创建一个请求工厂,来创建各种不同的请求,这里就提供了一个返回打开摄像头的请求.
<span style="font-family:Microsoft YaHei;font-size:14px;">public final class CameraRequestFactory { // Request types public static final int REQUEST_TYPE_OPEN_CAMERA = 0; // Response data public static final String BUNDLE_EXTRA_SWITCH = "cn.jesse.camera.datadroid.data.switch"; public static final String BUNDLE_EXTRA_OPEN_CAMERA = "cn.jesse.camera.datadroid.data.opencamera"; public static final String BUNDLE_EXTRA_RESULT = "com.foxykeep.datadroidpoc.extra.result"; public static final String BUNDLE_EXTRA_ERROR_MESSAGE = "com.foxykeep.datadroidpoc.extra.errorMessage"; public static Request getOpeningCameraOperation(int type){ Request request = new Request(REQUEST_TYPE_OPEN_CAMERA); request.setMemoryCacheEnabled(true); request.put(OpenCameraOperation.PARAM_METHOD, type); return request; } }</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">public class OpenCameraOperation implements Operation { private final String TAG = OpenCameraOperation.class.getSimpleName(); public static final String PARAM_METHOD = "cn.jesse.camera.extra.cameraType"; @Override public Bundle execute(Context context, Request request) throws ConnectionException, DataException, CustomRequestException { Log.i(TAG, "execute"); int cameraType = request.getInt(PARAM_METHOD); Log.i(TAG, "execute, camera type" + cameraType); Bundle bundle = new Bundle(); bundle.putInt(CameraRequestFactory.BUNDLE_EXTRA_SWITCH, CameraRequestFactory.REQUEST_TYPE_OPEN_CAMERA); bundle.putBoolean(CameraRequestFactory.BUNDLE_EXTRA_OPEN_CAMERA,true); return bundle; } }</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">public class CameraRequestService extends RequestService { @Override public Operation getOperationForType(int requestType) { switch (requestType) { case CameraRequestFactory.REQUEST_TYPE_OPEN_CAMERA: return new OpenCameraOperation(); } return null; } @Override protected Bundle onCustomRequestException(Request request, CustomRequestException exception) { if (exception instanceof MyCustomRequestException) { Bundle bundle = new Bundle(); bundle.putString(CameraRequestFactory.BUNDLE_EXTRA_ERROR_MESSAGE, "MyCustomRequestException thrown."); return bundle; } return super.onCustomRequestException(request, exception); } }</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">public abstract class DataDroidActivity extends FragmentActivity { private static final String SAVED_STATE_REQUEST_LIST = "savedStateRequestList"; protected CameraRequestManager mRequestManager; protected ArrayList<Request> mRequestList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRequestManager = CameraRequestManager.from(this); if (savedInstanceState != null) { mRequestList = savedInstanceState.getParcelableArrayList(SAVED_STATE_REQUEST_LIST); } else { mRequestList = new ArrayList<Request>(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList(SAVED_STATE_REQUEST_LIST, mRequestList); } protected void showBadDataErrorDialog() { new ErrorDialogFragmentBuilder(this).setTitle(R.string.dialog_error_data_error_title) .setMessage(R.string.dialog_error_data_error_message).show(); } }</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">public class CameraActivity extends DataDroidActivity implements ActionBar.OnNavigationListener, RequestListener, ConnectionErrorDialogListener{ private String TAG = CameraActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); Log.i(TAG, "CameraActivity has been created"); Request openCameraRequest = CameraRequestFactory.getOpeningCameraOperation(Definition.CameraType.Default); mRequestManager.execute(openCameraRequest, this); mRequestList.add(openCameraRequest); } @Override public void onRequestFinished(Request request, Bundle bundle) { if (mRequestList.contains(request)) { setProgressBarIndeterminateVisibility(false); mRequestList.remove(request); int choose = bundle.getInt(CameraRequestFactory.BUNDLE_EXTRA_SWITCH); switch(choose){ case CameraRequestFactory.REQUEST_TYPE_OPEN_CAMERA:{ boolean openCameraStatus = false; openCameraStatus = bundle.getBoolean(CameraRequestFactory.BUNDLE_EXTRA_OPEN_CAMERA); if(openCameraStatus){ Log.i(TAG, "open camera succeed"); }else{ } break; } } } } }</span>
标签:开源框架 android平台 数据管理 datadroid 框架
原文地址:http://blog.csdn.net/l2show/article/details/40787057