Android Open Source Framework Datadroid

Source: Internet
Author: User

Android Open Source Framework Datadroid

Datadroid is an open-source development library based on the Android platform, and the Android-based restful package simplifies data management in Android apps. The Open Source Library is a csdn resource.

Datadriod for use in Android projects
There are many types of data management demos from the source code on Github, and here is a simple way to describe the process of opening a camera through a datadroid workflow. Want to see the source of the following items can go to my GitHub view, address: GitHub
1. To create a request manager in a singleton mode, it is important to note that when you construct the parent class, you need to pass a requestservice to the request manager, which starts the service when you call Requestmanager's Execute method.
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 a variety of different requests, here is a request to return the camera to open.

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 a operation to open the camera, open the camera all the actions of this action are done in this operation, and after completion, you can use the bundle to callback the open result to the call activity.

Public class Opencameraoperation implements operation {Private final String TAG = OpenCameraOperation.class.getSimpleName ();p ublic 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 RequestService to provide the corresponding operation according to the 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. Establish datadroid activity to provide management of 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<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 (); }}
6. Build our own activity inherited from the above datadroidactivity, when want to open the camera, only need to get opencamera from the request factory request, with the request manager to execute the request, and put the request into the request queue .... The final Opencamera result will be obtained through the onrequestfinished callback, the whole 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; }            }        }}}


Android Open Source Framework Datadroid

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.