DataDroid 框架简介
DataDroid是基于Android平台的一个开源的开发库,基于Android 的RESTful封装用来简化Android应用中的数据管理.该开源库的下载地址为 CSDN资源.
DataDriod 在Android项目中的使用
从github上clone下来的源代码中就有很多种类型的数据管理的demo,这里就简单得描述一下通过DataDroid的工作流程进行一个打开摄像头的过程.想看下面项目的源码的可以去我的github查看,地址:Github
1.以单例模式建立一个请求管理器,这里需要注意的是在构造父类的时候,需要将一个RequestService传递给请求管理器,在调用RequestManager的execute方法时,会启动这个service.
<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>
3.创建一个打开摄像头的operation,打开摄像头这个动作的所有操作都在这个operation中完成,并且完成之后可以通过bundle来将打开的结果回调给调用的activity.
<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>
4.创建RequestService,根据不同的请求类型提供与之相对应的operation.
<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>
5.建立datadroid的activity,提供对datadroid 请求和请求队列的管理.
<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>
6.构建我们自己的activity继承自上面的DatadroidActivity, 在想要打开摄像头的时候,只需要从请求工厂中拿到openCamera的请求,用请求管理器来执行这个请求,并把该请求放入到请求队列中.....最后openCamera的结果就会通过onRequestFinished的回调来获取,整个过程还是比较简单的.
<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>
时间: 2024-10-19 13:19:23