Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 開發中使用開源框架 DataDroid

Android 開發中使用開源框架 DataDroid

編輯:關於Android編程

DataDroid 框架簡介

DataDroid是基於Android平台的一個開源的開發庫,基於Android 的RESTful封裝用來簡化Android應用中的數據管理.該開源庫的下載地址為 CSDN資源.

DataDriod 在Android項目中的使用

從github上clone下來的源代碼中就有很多種類型的數據管理的demo,這裡就簡單得描述一下通過DataDroid的工作流程進行一個打開攝像頭的過程.想看下面項目的源碼的可以去我的github查看,地址:Github
1.以單例模式建立一個請求管理器,這裡需要注意的是在構造父類的時候,需要將一個RequestService傳遞給請求管理器,在調用RequestManager的execute方法時,會啟動這個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.創建一個請求工廠,來創建各種不同的請求,這裡就提供了一個返回打開攝像頭的請求.
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;
    }
}

3.創建一個打開攝像頭的operation,打開攝像頭這個動作的所有操作都在這個operation中完成,並且完成之後可以通過bundle來將打開的結果回調給調用的activity.
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;
	}
}

4.創建RequestService,根據不同的請求類型提供與之相對應的operation.
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);
    }
}



5.建立datadroid的activity,提供對datadroid 請求和請求隊列的管理.
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.構建我們自己的activity繼承自上面的DatadroidActivity, 在想要打開攝像頭的時候,只需要從請求工廠中拿到openCamera的請求,用請求管理器來執行這個請求,並把該請求放入到請求隊列中.....最後openCamera的結果就會通過onRequestFinished的回調來獲取,整個過程還是比較簡單的.
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;
            }
            }
        }
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved