新建androidPlugin.js
! function(root, factory) {
if (typeof exports == 'object' && typeof module != 'undefined') {
module.exports = factory()
} else if (typeof define == 'function' && define.amd) {
define(factory)
} else {
document.addEventListener('plusready', function() {
var moduleName = 'AndroidBridge';
root.plus[moduleName] = factory()
}, false);
}
}(this, function() {
var _BARCODE = 'AndroidBridge';
var plugintest = {
CallAppFunc: function(num, successCallback, errorCallback) {
var success = typeof successCallback !== 'function' ? null : function(args) {
successCallback(args);
},
fail = typeof errorCallback !== 'function' ? null : function(code) {
errorCallback(code);
};
var callbackID = plus.bridge.callbackId(success, fail);
return plus.bridge.exec(_BARCODE, "callAppFunc", [callbackID, num]);
}
};
return plugintest;
});
vue页面调用
注意引用路径跟实际的一致
// 导入自己定义的插件js
var AndroidBridge = require('../../androidPlugin.js');
调用
AndroidBridge.CallAppFunc(
keyword,
function(result) {
let res = JSON.parse(result);
that.list = res;
// uni.showToast({title:JSON.stringify(result),icon:'none',duration:5000});
},
function(result) {
uni.showToast({title:result,icon:"none",duration:5000});
}
);
原生安卓项目
java下新增AndroidBridge类
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiSearch;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
public class AndroidBridge extends StandardFeature {
private static JSONArray tempArray;
private static IWebview iWebview;
private static String CallBackID;
/**
* @param pWebview
* @param array
*/
public void callAppFunc(IWebview pWebview, JSONArray array) {
this.tempArray = array;
this.iWebview = pWebview;
// 获取回调ID
this.CallBackID = array.optString(0);
// 获取参数并计算(这里模拟原生处理)
String newText = array.optString(1);
// 构建回传参数
// JSONArray newArray = new JSONArray();
String content = newText.trim();//获取自动提示输入框的内容
String city = "深圳" ;
InputtipsQuery inputtipsQuery = new InputtipsQuery(content, city);//初始化一个输入提示搜索对象,并传入参数
Inputtips inputtips = new Inputtips(pWebview.getActivity(), inputtipsQuery);//定义一个输入提示对象,传入当前上下文和搜索对象
inputtips.setInputtipsListener(new Inputtips.InputtipsListener() {
@Override
public void onGetInputtips(List<Tip> list, int errcode) {
Log.i("onGetInputtips: ",list.toString() + errcode);
if (errcode == 1000 && list != null) {
JSONArray newArray = new JSONArray();
ArrayList datas = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Tip tip = list.get(i);
//datas.add(tip);
LocationBean locationBean = new LocationBean();
locationBean.lat = tip.getPoint().getLatitude();
locationBean.lng = tip.getPoint().getLongitude();
locationBean.key = tip.getName();
locationBean.title = tip.getDistrict();
datas.add(locationBean);
}
// newArray.put(JSON.toJSONString(datas));
// 第一个参数是当前函数的入参,直接传入, 第二个是根据入参获取的回调id,第三个是回调的数据,是一个json数组
// JSUtil.execCallback(iWebview, CallBackID,newArray.toString(), JSUtil.OK, false);
JSUtil.execCallback(iWebview, CallBackID,JSON.toJSONString(datas), JSUtil.OK, false);
}
}
});//设置输入提示查询的监听,实现输入提示的监听方法onGetInputtips()
inputtips.requestInputtipsAsyn();//输入查询提示的异步接口实现
}
public class LocationBean {
public double lat ;
public double lng ;
public String key ;
public String title ;
}
}
添加feature
<feature name="AndroidBridge" value="AndroidBridge"/>
原生安卓项目加定位所需sdk和配置参数
本文也适用:UNIAPP 混合开发 JS调用安卓代码(ANDROID代码)