uniApp与原生Android 的交互

2021-06-25  本文已影响0人  xulj100

准备

1、创建uniapp项目

image04.png

3、在开发者中心配置Android证书签名


image06.png

4、本地打包


image07.png

2、uniapp与Android交互

{
  "nativePlugins": [
    {
      "plugins": [
        {
          "type": "module",
          "name": "AppModule",
          "class": "cn.hy.module.AppModule"
        }
      ]
    }
  ]
}
package cn.hy.module;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.hy.activity.MainActivity;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class AppModule extends UniModule {

    public static String TAG = "AppModule";

    public static int REQUEST_CODE = 1000;

    @UniJSMethod(uiThread = true)
    public void showToast(String msg){
        if(mUniSDKInstance!= null && mUniSDKInstance.getContext() instanceof Activity){
            Toast.makeText(mUniSDKInstance.getContext(),msg,Toast.LENGTH_SHORT).show();
        }
    }

    @UniJSMethod(uiThread = true)
    public void sendMsg(String message, UniJSCallback callback){
        if(mUniSDKInstance!= null && mUniSDKInstance.getContext() instanceof Activity){
            message = message + "finished";
            callback.invoke(message);
            Map<String, Object> params = new HashMap<>();
            params.put("code","0");
            mUniSDKInstance.fireGlobalEventCallback("MyEvent",params);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @UniJSMethod(uiThread = true)
    public void gotoNativePage(){
        if(mUniSDKInstance!= null &amp;&amp; mUniSDKInstance.getContext() instanceof Activity){
            Intent intent = new Intent(mUniSDKInstance.getContext(), MainActivity.class);
           ((Activity)mUniSDKInstance.getContext()).startActivityForResult(intent,REQUEST_CODE);
        }
    }

}
<template>
    <view class="content">
        <image class="logo" src="/static/logo.png"></image>
        <button class="btn" @click="showToast(title)">原生toast</button>
        <button class="btn"  @click="sendMessage(jsData)">sendMessage</button>
        <button class="btn"  @click="gotoNativePage()">gotoNativePage</button>
    </view>
</template>

<script>
    var appModule = uni.requireNativePlugin("AppModule");
    export default {
        data() {
            return {
                title: 'vue.js',
                jsData: 'data from vue ' 
            }
        },
        onLoad() {
            const that = this;
            plus.global.addEventListener("MyEvent",function(result){
                uni.showToast({
                    title: 'Hello MyEvent '
                });
            })
        },
        methods: {
                showToast(msg){
                    appModule.showToast(msg);
                },
                sendMessage(data){
                    appModule.sendMsg(data,ret=>{
                         appModule.showToast(ret);
                    })
                },
                gotoNativePage(){
                    appModule.gotoNativePage();
                }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
        margin: 20rpx;
    }

    .btn {
        margin: 20rpx;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

结尾

如果还有问题,可以下载源码:https://github.com/xulj-tech/HBuilder-Integrate-AS.git,uniapp文件下有个压缩包是是对应的uniapp项目。

上一篇下一篇

猜你喜欢

热点阅读