import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
// 每次做新插件的时候需要继承Plugin 然后添加装饰器 @CapacitorPlugin
@CapacitorPlugin(name = "MyPlugin",permissions = {})
public class MyPlugin extends Plugin {
/**
* 插件初始化
* https://capacitorjs.com/docs/plugins/android#running-code-on-plugin-load
*/
@Override
public void load() {
Log.wtf("MyPlugin" + getClass().getSimpleName(), "MyPluginPlugin init");
}
@PluginMethod()
public void getName(PluginCall call) {
String value = call.getString("value");
String result = "";
JSObject ret = new JSObject();
try {
result = authenticate.getStaffName();
ret.put("result", result);
} catch (Exception exception) {
call.reject(exception.getLocalizedMessage(), null, exception);
}
call.resolve(ret);
}
}
3.x注册方式
public class MainActivity extends BridgeActivity {
public Authenticate authenticate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerPlugin(MyPlugin.class);
authenticate = new Authenticate(getApplicationContext(), getString(R.string.my_package_name));
}
}
3.x调用方式
// Device Info
import { Device } from "@capacitor/device/dist/esm/index";
async function getDeviceInfo() {
const info = await Device.getInfo();
alertMessage(JSON.stringify(info))
}
async function getBatteryInfo() {
const info = await Device.getBatteryInfo();
alertMessage(JSON.stringify(info))
// showToast(JSON.stringify(info));
}
// 插件测试代码
import { Capacitor } from "@capacitor/core";
console.log("Capacitor", Capacitor);
async function getMyPlugin() {
const a = Capacitor.Plugins.MyPlugin.getName()
console.log("Capacitor-MyPlugin", a);
}
getMyPlugin()