uniapp 安卓调用高德API实现关键字列表(JS调用安卓原生)

新建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代码)

参考

参考1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wx_h13813744

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值