想要使用appinventor标准支持的以外功能只有两种方式。
1.直接修改appinventor的源码,添加新的功能,然后使用这个私人定制的服务器。
2.编写拓展,可以添加到任何可以正常使用的appinventor服务上。
第二种的优势很明显,官方支持的拓展形式,让使用者免去了搭建appinventor服务的功夫。
拓展开发环境如果已经搭建出了标准的appinventor服务,可以直接使用该环境进行开发。
但是如果你还没有搭建appinventor服务,由于该整体的服务搭建过程毕竟慢,且包含多余的服务部分(2G多),所以我们可以使用专门做extension的开发环境:
GitHub - mit-cml/extension-template: Template repository for creating App Inventor extensionshttps://github.com/mit-cml/extension-template
基础开发步骤首先下载该库:
git clone --recurse-submodules https://github.com/mit-cml/extension-template.git my-extension
然后在src文件夹内新建MyExtension.java项目:
package cn.temp;
import com.google.appinventor.components.annotations.*;//注解
import com.google.appinventor.components.common.ComponentCategory; //组件类别
import com.google.appinventor.components.runtime.*;
ava的实用工具类库java.util包。在这个包中,Java提供了一些实用的方法和数据结构。例如,Java提供日期(Data)类、日历(Calendar)类来产生和获取日期及时间,提供随机数(Random)类产生各种类型的随机数,还提供了堆栈(Stack)、向量(Vector) 、位集合(Bitset)以及哈希表(Hashtable)等类来表示相应的数据结构。
import com.google.appinventor.components.runtime.util.*;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
@DesignerComponent(version = MyExtension.VERSION, //设计器组件
description = "by Aheadtechs.", //备注,描述
category = ComponentCategory.EXTENSION, //类别:展示在appinventor的哪个模块下
nonVisible = true, //不可见
iconName = "images/extension.png") //组件图标
@SimpleObject(external = true) //外部插件
public class MyExtension extends AndroidNonvisibleComponent {
public static final int VERSION = 1; //如果一个数据既是static又是final,那么它会拥有一块无法改变的存储空间
private static final String LOG_TAG = "MyExtension";
public MyExtension(ComponentContainer container) { //记忆技巧:contain 包含,容纳 + er 表物 → 容器
//调用父类的属性或方法可以通过super关键字。通过super来获取父类的私有属性
super(container.$form());
}
//计算a+b的和然后转换成字符串
@SimpleFunction(description = "add up a and b")
public String addab(int a,int b) {
return ""+(a+b);
}
}
然后就可以调用:
ant extensions
编译成功之后就会在项目的根目录生成一个 out文件夹,其中便有我们需要的.aix后缀的文件。
注意拓展的形式就是.aix后缀的文件。
高级开发
这里虽然可以满足我们的基础需求,但是真实开发的过程中不仅仅需要这个的简单例子,我们可能需要第三方库的添加!
例如一个特殊的通信协议:opcua之类的。
具体步骤如下:
1.下载对应的库的jar文件,并拷贝到lib/deps文件夹:
然后在Myextension.java文件中添加:
@SimpleObject(external = true) //外部插件
//这个位置不能放上面!必须在SimpleObject下面
@UsesLibraries(libraries = "opc-ua-stack-1.4.1-224.jar,slf4j-api-1.7.0.jar")
重新进行编译,就可以将该jar的内容添加到生成的.aix文件中,从文件的大小就可以轻松看出。
调用就和普通调用一样了:
import org.opcfoundation.ua.application.Client;
...
参考文档:
https://saitwalshreyash19.medium.com/writing-extensions-for-app-inventor-2-and-kodular-7d20092bff16https://saitwalshreyash19.medium.com/writing-extensions-for-app-inventor-2-and-kodular-7d20092bff16
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)