赞
踩
在您阅读此文档时,我们假定您已经具备了相应Android应用开发经验,使用Android Studio开发过Android原生。也应该对HTML,JavaScript,CSS等有一定的了解, 并且熟悉在JavaScript和JAVA环境下的JSON格式数据操作等。
为了插件开发者更方便快捷的开发uni原生插件!2.9.8版本起修改了uni插件开发API及规范。当然还会继续兼容老的插件运行及开发。推荐插件开发者按新版规范实现开发插件。方便日后更高效的更新迭代uni原生插件!
开发的插件必须导入uniapp-v8-release.aar,创建一个插件的module(本例以通知插件NotificationModule为例),插件开发有两种类型。
1、Module 扩展 非 UI 的特定功能.
2、Component 扩展 实现特别功能的 Native 控件
- //必须添加的依赖
- compileOnly 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0',
- compileOnly 'androidx.core:core:1.1.0'
- compileOnly 'androidx.fragment:fragment:1.1.0'
- compileOnly 'androidx.appcompat:appcompat:1.1.0'
- compileOnly 'androidx.recyclerview:recyclerview:1.1.0'
- compileOnly 'com.alibaba:fastjson:1.2.83'
-
- compileOnly fileTree(include: ['uniapp-v8-release.aar'], dir: '../app/libs')
-keep public class * extends io.dcloud.feature.uniapp.common.UniModule{*;}
- public class NotificationModule extends UniModule {
- /**
- * 发送通知
- * @param option
- * @param callback
- */
- @UniJSMethod(uiThread = true)
- public void sendNotice(JSONObject option, UniJSCallback callback){
- if (option==null){
- callback.invoke(PluginResultEntites.fail(-1,"参数不能为空"));
- return;
- }
- if (!option.containsKey("title")){
- callback.invoke(PluginResultEntites.fail(-1,"需要设置title参数(通知title)"));
- return;
- }
- if (TextUtils.isEmpty(option.getString("title"))){
- callback.invoke(PluginResultEntites.fail(-1,"参数title不能为空(通知title)"));
- return;
- }
- if (!option.containsKey("content")){
- callback.invoke(PluginResultEntites.fail(-1,"需要设置content参数(通知content)"));
- return;
- }
- if (TextUtils.isEmpty(option.getString("content"))){
- callback.invoke(PluginResultEntites.fail(-1,"参数content不能为空(通知content)"));
- return;
- }
- int resId = mUniSDKInstance.getContext().getResources().getIdentifier("ic_launcher", "mipmap", mUniSDKInstance.getContext().getPackageName());
- String title = option.getString("title");
- String content = option.getString("content");
- Intent intent = new Intent(mUniSDKInstance.getContext(), NotificationClickReceiver.class);
- intent.putExtra("type",10);
- intent.putExtra("noticeTitle",title);
- intent.putExtra("noticeContent",content);
- intent.putExtra("appID",option.getString("appID"));
- intent.putExtra("noticeExtras",option.containsKey("extras")?option.getString("extras"):"");
- // Intent intent = mUniSDKInstance.getContext().getPackageManager().getLaunchIntentForPackage(mUniSDKInstance.getContext().getPackageName());
- //普通通知栏消息
- NotificationUtils notificationUtils = new NotificationUtils(mUniSDKInstance.getContext(), 0, "13214345353", resId, title, content);
- notificationUtils.notifiedReceive(intent);
- callback.invoke(PluginResultEntites.success());
-
- }
- }
下面以TestComponent
为例
- public class TestText extends UniComponent<TextView>{
- //创建对象
- @Override
- protected TextView initComponentHostView(@NonNull 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);
- }
-
- //清空电话号码
- @UniJSMethod
- public void clearTel() {
- getHostView().setText("");
- }
-
- }
- <template>
- <div>
- <myText ref="telText" tel="12305" style="width:200;height:100" @onTel="onTel" @click="myTextClick"></myText>
- </div>
- </template>
- <script>
- export default {
- methods: {
- myTextClick(e) {
- this.$refs.telText.clearTel();
- }
- }
- }
- </script>
主要介绍json的方式注册新创建的插件,现在新建截图的文件,在主项目的app asset目录下创建。
在dcloud_uniplugins.json中注册新建的插件
- {
- "plugins": [
- {
- "type": "module",
- "name": "NotificationModule",
- "class": "com.kairison.applet.plugin.eachother.notice.NotificationModule"
- }
- ]
- }
在uni-app项目中获取插件,通过requireNativePlugin 来获取插件,本例子以NotificationModule
- const notificationModule = uni.requireNativePlugin('NotificationModule')
- notificationModule.sendNotice({
- title: '测试通知',
- content: '测试内容',
-
- }, (res) => {
- console.log(JSON.stringify(res.data))
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。