---
title: "Android本地APP集成Mui框架"
author: "zhy20045923"
date: "2016-01-26T14:41:56Z"
tags: []
categories: ["移动开发"]
slug: "android-app-mui"
draft: false
lastmod: "2016-01-26T14:41:56Z"
id: 2273930
url: "https://null-point.iteye.com/blog/2273930"
---

2.如何在安卓原生APP中使用MUI框架
2.1资源准备
1.需要去首先从官网http://www.dcloud.io/ 下载HBuilder及其他的开发工具（eclipse或者Android studio）
2.到http://ask.dcloud.net.cn/article/103 下载HBuilder离线打包Android版SDK
2.2项目集成
1. 用Android Studio创建一个新项目，如MyFirstMUI
2. 导入jar包，可从HBuilder-Integrate工程的libs中选择，具体要导入哪些jar包可从SDK中的Feature-Android.xls中查看。
3.加入app资源
①在工程目录下的创建assets文件夹，并在assets目录下添加apps目录
②在apps目录下创建应用ID同名目录
③在应用ID同名目录下创建www目录
④将应用资源和应用配置文件（manifest.json）拷贝到www目录下
其中“H5B1EA68D”目录名称为应用manifest.json中的id名称，务必一致。
如下图：
 
4.添加配置文件
添加control.xml文件和properties.xml文件到assets->data目录下，其中control.xml文件的appid值为HBuilder应用的appid，必须与应用manifest.json中的id值完全一致；appver为应用的版本号，用于应用资源的升级，必须保持与manifest.json中的version -> name值完全一致；version值为应用基座版本号（plus.runtime.innerVersion返回的值），不要随意修改。
Properites文件用于对应JS类名和Android包名的对应关系，用户可以根据开发项目使用到的插件对文件进行添加或删改。
这两个文件都可从HBuilder-Integrate工程中拷贝过来进行修改。如下图：
 
5.复制所需的资源文件到drawable和layout文件夹中
如下图：
 

6、修改AndroidManifest.xml文件
文件中的versionCode与manifest.json中version -> code值一致；versionName与manifest.json中version -> name值一致。根据HBuilder-Integrate工程中的AndroidManifest.xml文件添加所的功能权限。
7. 添加代码编译测试
如下图：
 
注意RInfomation要连同其目录一起复制进项目中。
2.3参考资料
http://www.mamicode.com/info-detail-1149716.html
http://ask.dcloud.net.cn/docs/
3.本地app如何与H5+交互
1.JS扩展方法的实现
document.addEventListener("plusready", function() {
	var _BARCODE = 'plugintest', B = window.plus.bridge;
	var plugintest = {
		PluginTestFunction : function(Argus1, Argus2, Argus3, Argus4,
				successCallback, errorCallback) {
			var success = typeof successCallback !== 'function' ? null
					: function(args) {
						successCallback(args);
					}, fail = typeof errorCallback !== 'function' ? null
					: function(code) {
						errorCallback(code);
					};
			callbackID = B.callbackId(success, fail);

			return B.exec(_BARCODE, "PluginTestFunction", );
		},
		PluginTestFunctionArrayArgu : function(Argus, successCallback,
				errorCallback) {
			var success = typeof successCallback !== 'function' ? null
					: function(args) {
						successCallback(args);
					}, fail = typeof errorCallback !== 'function' ? null
					: function(code) {
						errorCallback(code);
					};
			callbackID = B.callbackId(success, fail);
			return B.exec(_BARCODE, "PluginTestFunctionArrayArgu", );
		},
		PluginTestFunctionSync : function(Argus1, Argus2, Argus3, Argus4) {
			return B.execSync(_BARCODE, "PluginTestFunctionSync", );
		},
		PluginTestFunctionSyncArrayArgu : function(Argus) {
			return B.execSync(_BARCODE, "PluginTestFunctionSyncArrayArgu",
					);
		}
	};
	window.plus.plugintest = plugintest;
}, true);
2.Html使用示例		plus.plugintest.PluginTestFunctionArrayArgu(, function(result) {alert( result );},function(result){alert(result)});
			plus.plugintest.PluginTestFunction("Html5","Plus","AsyncFunction","MultiArgument!", function( result ) {alert( result  + "_" + result  + "_" + result  + "_" + result );},function(result){alert(result)});
			var Argus = plus.plugintest.PluginTestFunctionSyncArrayArgu();
			var resulrt = plus.plugintest.PluginTestFunctionSync("Html5","Plus","SyncFunction","MultiArgument!");
3. Android第三方插件开发
创建插件类
创建一个继承自StandardFeature的类，实现第三方插件扩展。
创建插件类需要引入的包 
import io.dcloud.DHInterface.IWebview;
import io.dcloud.DHInterface.StandardFeature;
import io.dcloud.util.JSUtil;
实现扩展方法
package com.example.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;

public class TestUtil extends StandardFeature {
	
	public void PluginTestFunction(IWebview pWebview, JSONArray array) {
		String CallBackID = array.optString(0);
		JSONArray newArray = new JSONArray();
		newArray.put(array.optString(1));
		newArray.put(array.optString(2));
		newArray.put(array.optString(3));
		newArray.put(array.optString(4));
		JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);
	}

	public void PluginTestFunctionArrayArgu(IWebview pWebview, JSONArray array) {
		String ReturnString = null;
		String CallBackID = array.optString(0);
		JSONArray newArray = null;
		try {
			newArray = new JSONArray(array.optString(1));
			String inValue1 = newArray.getString(0);
			String inValue2 = newArray.getString(1);
			String inValue3 = newArray.getString(2);
			String inValue4 = newArray.getString(3);
			ReturnString = inValue1 + "-" + inValue2 + "-" + inValue3 + "-" + inValue4;
		} catch (JSONException e) {
			e.printStackTrace();
		}

		JSUtil.execCallback(pWebview, CallBackID, ReturnString, JSUtil.OK, false);
	}

	public String PluginTestFunctionSyncArrayArgu(IWebview pWebview, JSONArray array) {
		JSONArray newArray = null;
		JSONObject retJSONObj = null;
		try {
			newArray = array.optJSONArray(0);
			String inValue1 = newArray.getString(0);
			String inValue2 = newArray.getString(1);
			String inValue3 = newArray.getString(2);
			String inValue4 = newArray.getString(3);

			retJSONObj = new JSONObject();
			retJSONObj.putOpt("RetArgu1", inValue1);
			retJSONObj.putOpt("RetArgu2", inValue2);
			retJSONObj.putOpt("RetArgu3", inValue3);
			retJSONObj.putOpt("RetArgu4", inValue4);

		} catch (JSONException e1) {
			e1.printStackTrace();
		}

		return JSUtil.wrapJsVar(retJSONObj);
	}

	public String PluginTestFunctionSync(IWebview pWebview, JSONArray array) {
		String inValue1 = array.optString(0);
		String inValue2 = array.optString(1);
		String inValue3 = array.optString(2);
		String inValue4 = array.optString(3);

		String ReturnValue = inValue1 + "-" + inValue2 + "-" + inValue3 + "-" + inValue4;
		return JSUtil.wrapJsVar(ReturnValue, true);
	}
}
4.关联JS插件名和原生类
在编写扩展插件时需要修改“/assets/data”中properties.xml文件，在其中添加JS对象名称和Android包的类名对应关系，SDK会根据对应的类名查找并生成相应的对象并执行对应的逻辑。
<feature 
name="plugintest" value="com.example.test.TestUtil">
</feature>
在应用的manifest.json文件中还需要添加扩展插件的应用使用权限
"plugintest": {
	"description": "访问本地SDK"
},
5.参考资料
http://ask.dcloud.net.cn/article/66