Android与uni-app 互相通信案例(包含源代码)

一、背景

在uni-app开发过程中,有时候会遇到uni-app插件或者提供的api对硬件操作不太友好,需要使用原生Android开发对应模块,为了使得双方通信方便,特意封装了一个接口,可实现Android与Uni-app互相通讯。

二、内容

做完以下第一、第二部分,即可实现Android与uni-app 互相通信,当然双方通信有不同方式,具体情况具体分析,我的采用的方案是写Android原生插件,在uni-app中集成Android原生插件方式实现。

1、原生Android部分

Android原生涉及到的几个类,MyEvent.java(传递消息的ben类),MyEventManager.java类作用(事件管理器用来处理事件与通知监听器),MyListener.java类,作用接口回调(监听器接口)。由于采用的是uni-app插件方式,故先去uni-app官网下载插件sdk进行开发。详情见最后的代码连接,以下只是例举用的类。

代码


public class MyEvent {

    //数据
    private Object data;

    //事件来源  字符串
    private String source;

    //触发对象
    private Object trigger;

    private int state;

    public MyEvent() {
    }

    public MyEvent(Object data) {
        this.data = data;
    }

    public Object getTrigger() {
        return trigger;
    }

    public void setTrigger(Object trigger) {
        this.trigger = trigger;
    }

    public MyEvent(Object data, String source) {
        this.data = data;
        this.source = source;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "MyEvent{" +
                "data=" + data +
                ", source='" + source + '\'' +
                ", trigger=" + trigger +
                ", state=" + state +
                '}';
    }

}

MyEventManager.java


/**
 * 事件管理器
 * 用来
 * 处理
 * 事件
 * 与通知
 * 监听器
 */
public class MyEventManager {

    private static MyEventManager myEventManager;

    private Map> listeners;

    /**
     * 不能外部 new
     * 实例化
     */
    private MyEventManager() {
        this.listeners = new HashMap>();
    }

    /**
     * 返回监听 总数
     *
     * @return
     */

    public int getSize() {
        int size = 0;
        for (String str : listeners.keySet()) {
            size = size + listeners.get(str).size();

        }
        return size;
    }

    public Map> getListeners() {

        return listeners;
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static MyEventManager getMyEventManager() {
        if (myEventManager == null) {
            synchronized (MyEventManager.class) {
                if (myEventManager == null) {
                    myEventManager = new MyEventManager();
                }
            }
        }
        return myEventManager;
    }

    /***
     * 添加事件
     * @param listener    事件对象
     * @param source      来源
     */
    public MyListener addListener(MyListener listener, String source) {

        if (listener != null && source != null) {
            Collection myListeners = listeners.get(source);

            if (myListeners == null) {
                myListeners = new HashSet();
                listeners.put(source, myListeners);
            }
            myListeners.add(listener);

        }
        return listener;

    }

    /***
     * 添加事件
     *  @param source      来源
     * @param listener    事件对象
     */
    public MyListener addListener(String source, MyListener listener) {

        return addListener(listener, source);
    }

    /**
     * 移除监听
     *
     * @param listener
     */
    public void removeListener(MyListener listener) {
        if (listeners == null || listener == null) {
            return;
        }

        //变量所有  找出相同的  删除
        for (String str : listeners.keySet()) {

            Collection collection = listeners.get(str);
            Iterator iter = collection.iterator();
            while (iter.hasNext()) {
                MyListener next = (MyListener) iter.next();
                if (next == listener) {
                    collection.remove(next);
                    return;
                }
            }
        }

    }

    /***
     *   发送数据
     * @param data   数据
     * @param source 来源
     * @return
     */
    public static MyEvent postMsg(Object data, String source) {
        MyEventManager myEventManager = MyEventManager.getMyEventManager();
        MyEvent myEvent = new MyEvent(data);
        myEvent.setSource(source);
        if (myEventManager.listeners == null)

            return myEvent;
        myEventManager.notifyListeners(myEvent, myEvent.getSource());
        return myEvent;
    }

    /**
     * 通知所有的myListener
     * 相同的 (source) 来源才通知
     */
    private void notifyListeners(MyEvent event, String source) {

        //取出  key为source 的  监听器集合
        Collection collection = listeners.get(source);

//        Log.i(MyEventManager.class.getName(), source + "--->" + event.getData());

        if (collection == null) {
            return;
        }
        //遍历监听器集合
        Iterator iter = collection.iterator();
        while (iter.hasNext()) {
            MyListener next = iter.next();
            //通知回调
            next.onChange(event);
        }

        //销毁事件对象
        event = null;
    }

}

MyListener.java类

public interface MyListener extends EventListener {
    void onChange(MyEvent myEvent);
}

BluetoothAndWifi.java类继承UniModule实现插件


public class BluetoothAndWifi extends UniModule {

    public static int REQUEST_CODE = 1000;//请求码
    public static int REQUEST_BLUETOOTH_CODE = 1001;//请求码

    @UniJSMethod(uiThread = false)
    public void sendMsgToUni() {
        MyEventManager.postMsg("==大宝==Android发送给==Uni-app====", "formAndroid");
    }

    @UniJSMethod(uiThread = false)
    public void initAndroidEventListner() {

        Ted();
    }

    @UniJSMethod(uiThread = true)
    public void Ted() {
        Toast.makeText(mWXSDKInstance.getContext(), "==初始化监听AndroidEventListner==", Toast.LENGTH_SHORT).show();

        MyEventManager.getMyEventManager().addListener(new MyListener() {
            @Override
            public void onChange(MyEvent myEvent) {

                if (myEvent.getSource().trim().equals("fromUni-app") ) {//判断过滤
                    //从uniapp 或者原生传过来的数据
                    Object data = myEvent.getData();
                    Toast.makeText(mWXSDKInstance.getContext(), "==Uni传过来的数据====" + data, Toast.LENGTH_SHORT).show();
                }

            }
        }, "fromUni-app");
    }

    /**
     * 加法运算
     */
    @UniJSMethod(uiThread = false)
    public void addMathNumber(int a, int b, UniJSCallback callback) {
        callback.invoke(a + b + 9);
    }

    /**
     * 原生toast提示
     */
    @UniJSMethod(uiThread = false)
    public void showToast(String msg, UniJSCallback callback) {
        Toast.makeText(mWXSDKInstance.getContext(), msg, Toast.LENGTH_SHORT).show();
        callback.invoke(msg);
    }

    /**
     * 原生toast提示
     */
    @UniJSMethod(uiThread = false)
    public void aShowToastOK(String msg, UniJSCallback callback) {
        Toast.makeText(mWXSDKInstance.getContext(), msg, Toast.LENGTH_SHORT).show();
        callback.invoke(msg);
    }

    /**
     * 跳转到
     * wifi页面
     */
    @UniJSMethod(uiThread = false)
    public void gotoWifiPage() {

        if (mUniSDKInstance != null && mUniSDKInstance.getContext() instanceof Activity) {
            Intent intent = new Intent(mUniSDKInstance.getContext(), WiFiActivity.class);
            ((Activity) mUniSDKInstance.getContext()).startActivityForResult(intent, REQUEST_CODE);
        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && data.hasExtra("respond")) {
//            Log.e("TestModule", "原生页面返回----"+data.getStringExtra("respond"));
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

怎么使用

 Android原生部分
 //发送消息
 MyEventManager.postMsg("==大宝==Android发送给==Uni-app====", "formAndroid");//发消息

 //初始化监听
      MyEventManager.getMyEventManager().addListener(new MyListener() {
            @Override
            public void onChange(MyEvent myEvent) {

                if (myEvent.getSource().trim().equals("fromUni-app") ) {//判断过滤
                    //从uniapp 或者原生传过来的数据
                    Object data = myEvent.getData();
                    Toast.makeText(mWXSDKInstance.getContext(), "==Uni传过来的数据====" + data, Toast.LENGTH_SHORT).show();
                }

            }
        }, "fromUni-app");
//"fromUni-app" 标记 必须与发送消息的标记一样


uni-app部分的代码

//获取java类 
let myEventManager = plus.android.importClass("com.xiaoya.mylibrary.business.msg.MyEventManager");
let eventManager = myEventManager.getMyEventManager();

let myListener = plus.android.implements("com.xiaoya.mylibrary.business.msg.MyListener", {
                    onChange: function(event) {
                        plus.android.importClass(event);
                        console.log("==日志输出33333==" + event.getData());
                        if (event.getSource() == 'formAndroid') {


                            andoridModule.showToast(event.getData());
                        }

                        //导入类  

                        //获取数据  
                        // console.log);  
                        //获取来源  
                        // console.log(event.getSource();  
                    }
                })

  // //添加监听器    
  eventManager.addListener("formAndroid", myListener);



msg部分

Android与uni-app 互相通信案例(包含源代码)_第1张图片

2、uni-app调用部分

a、代码






uni-app工程截图

Android与uni-app 互相通信案例(包含源代码)_第2张图片

3、最终效果

Android与uni-app 互相通信案例(包含源代码)_第3张图片

4、代码地址

代码还包含其他功能,例如uni-app调用

Android原生代码

https://download.csdn.net/download/shi450561200/87383612

uni-app代码

https://download.csdn.net/download/shi450561200/87383617

https://ask.dcloud.net.cn/article/id-36187

你可能感兴趣的:(uni-app,Android,android,uni-app)