uni-app(四):原生插件开发(Android)

原生插件开发

原生插件开发

主要分为两类扩展:

Module: 扩展非 UI 的特定功能

Component: 扩展实现特别功能的 Native 控件

module

1.创建模块

2.解决报错

3.修改依赖

4.编写插件代码

java 复制代码
package com.test.uniplugin_module;

import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class TestModule extends UniModule {
    String TAG = "TestModule";
    public static int REQUEST_CODE = 1000;

    //run ui thread
    @UniJSMethod(uiThread = true)
    public void testAsyncFunc(JSONObject options, UniJSCallback callback) {
        Log.e(TAG, "testAsyncFunc--" + options);
        if (callback != null) {
            JSONObject data = new JSONObject();
            data.put("code", "success");
            callback.invoke(data);
            //callback.invokeAndKeepAlive(data);
        }
    }

    //run JS thread
    @UniJSMethod(uiThread = false)
    public JSONObject testSyncFunc() {
        JSONObject data = new JSONObject();
        data.put("code", "success");
        return data;
    }

    @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);
        }
    }
}

5.添加插件配置

6.引入模块

7.调用插件代码

html 复制代码
<template>
  <view>
    <button type="primary" @click="test()">插件调用</button>
  </view>
</template>

<script>
  export default {
    data() {
      return {};
    },
    onLoad() {},
    methods: {
      test() {
        const testModule = uni.requireNativePlugin("TestModule");
        testModule.testAsyncFunc(
          {
            name: "Lee",
            age: 18,
          },
          (res) => {
            uni.showToast({
              icon: "none",
              title: JSON.stringify(res),
            });
          }
        );
      },
    },
  };
</script>

8.运行

component

1.创建模块

2.解决报错

与上同

3.修改依赖

gradle 复制代码
dependencies {
    compileOnly fileTree(dir: 'libs', include: ['*.jar'])
    compileOnly fileTree(dir: '../simpleDemo/libs', include: ['uniapp-v8-release.aar'])
}

4.编写插件代码

java 复制代码
package com.test.uniplugin_component;

import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;

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

import io.dcloud.feature.uniapp.UniSDKInstance;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.ui.action.AbsComponentData;
import io.dcloud.feature.uniapp.ui.component.AbsVContainer;
import io.dcloud.feature.uniapp.ui.component.UniComponent;
import io.dcloud.feature.uniapp.ui.component.UniComponentProp;

public class TestComponent extends UniComponent<TextView> {

    public TestComponent(UniSDKInstance instance, AbsVContainer parent, AbsComponentData basicComponentData) {
        super(instance, parent, basicComponentData);
    }

    @Override
    protected TextView initComponentHostView(Context context) {
        TextView textView = new TextView(context);
        textView.setTextSize(20);
        textView.setTextColor(Color.BLACK);
        return textView;
    }

    @UniComponentProp(name = "tel")
    public void setTel(String telNumber) {
        getHostView().setText("tel: " + telNumber);
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> number = new HashMap<>();
        number.put("tel", telNumber);
        //目前uni限制 参数需要放入到"detail"中 否则会被清理
        params.put("detail", number);
        fireEvent("onTel", params);
    }

    @UniJSMethod
    public void clearTel() {
        getHostView().setText("");
    }

    @Override
    public void onActivityResume() {
        super.onActivityResume();
    }

    @Override
    public void onActivityPause() {
        super.onActivityPause();
    }

    @Override
    public void onActivityDestroy() {
        super.onActivityDestroy();
    }
}

5.添加插件配置

6.引入模块

7.调用插件代码

注意:需要将.vue文件修改为.nvue文件

html 复制代码
<template>
	<view>
		<TestComponent ref="telText" tel="12305" style="width:200px;height:100px;" @onTel="onTel" @click="myTextClick"></TestComponent>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {

		},
		methods: {
			onTel(e) {
				console.log("onTel=" + e.detail.tel);
			},
			myTextClick(e) {
				this.$refs.telText.clearTel();
			},
		},
	}
</script>

8.运行

相关推荐
烬奇小云3 小时前
认识一下Unicorn
android·python·安全·系统安全
_处女座程序员的日常7 小时前
Rollup failed to resolve import “destr“ from ***/node_modules/pinia-plugin-pers
javascript·uni-app·vue
顾北川_野15 小时前
Android 进入浏览器下载应用,下载的是bin文件无法安装,应为apk文件
android
CYRUS STUDIO15 小时前
Android 下内联汇编,Android Studio 汇编开发
android·汇编·arm开发·android studio·arm
右手吉他15 小时前
Android ANR分析总结
android
慢慢雨夜16 小时前
uniapp打包华为,提示请提供64位版本软件包后再提交审核
uni-app
秋雨凉人心17 小时前
uniapp 设置安全区域
前端·javascript·vue.js·uni-app
Little_Code17 小时前
uniapp 如何使用vuex store (亲测)
uni-app·vuex·store
咸虾米_17 小时前
uniCloud云对象调用第三方接口,根据IP获取用户归属地的免费API接口,亲测可用
网络·uni-app·unicloud
PenguinLetsGo17 小时前
关于 Android15 GKI2407R40 导致梆梆加固软件崩溃
android·linux