Use the open-source framework DataDroid in Android Development
DataDroid framework overview DataDroid is an open-source development library based on the Android platform. The RESTful package based on Android is used to simplify data management in Android applications. The open-source library is a CSDN resource.
There are many types of data management demos in the source code cloned from github In the Android project, here we will briefly describe the process of opening a camera through the DataDroid workflow. if you want to view the source code of the following project, go to my github. Address: Github
1. create a request manager in singleton mode. Note that when constructing a parent class, you must pass a RequestService to the request manager. When you call the execute method of RequestManager, will start this service.
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);}}
2. Create a request factory to create different requests. Here, a request to open the camera is provided.
public final class CameraRequestFactory {// Request typespublic 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; }}
3. Create an operation to open the camera. All operations to open the camera are completed in this operation. After the operation is completed, you can use bundle to call back the opened result to the called activity.
public class OpenCameraOperation implements Operation {private final String TAG = OpenCameraOperation.class.getSimpleName();public static final String PARAM_METHOD = "cn.jesse.camera.extra.cameraType";@Overridepublic 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;}}
4. Create a RequestService and provide the corresponding operation based on different request types.
public class CameraRequestService extends RequestService {@Overridepublic 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); }}
5. Create a datadroid activity to manage datadroid requests and request queues.
public abstract class DataDroidActivity extends FragmentActivity { private static final String SAVED_STATE_REQUEST_LIST = "savedStateRequestList"; protected CameraRequestManager mRequestManager; protected ArrayList
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
(); } } @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(); }}
6. building our own activity inherits from the above DatadroidActivity. To open the camera, you only need to get the openCamera request from the request factory and use the request manager to execute the request, and put the request into the request queue ..... finally, the results of openCamera will be obtained through the onRequestFinished callback. The entire process is relatively simple.
public class CameraActivity extends DataDroidActivityimplements ActionBar.OnNavigationListener, RequestListener, ConnectionErrorDialogListener{private String TAG = CameraActivity.class.getSimpleName();@Overrideprotected 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);}@Overridepublic 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; } } }}}