简体   繁体   English

离子电容器插件不适用于 android

[英]Ionic capacitor plugin not working for android

I am written a very simple plugin which get all the messages from android device and pack it to response.我编写了一个非常简单的插件,它从 android 设备获取所有消息并将其打包以响应。 When I am using npm install ionic-capacitor-sms-access and trying to access messages it just call web method but not the android method.当我使用npm install ionic-capacitor-sms-access并尝试访问消息时,它只调用 web 方法而不是 android 方法。 So its not working can someone help me here and tell me what went wrong?所以它不起作用有人可以在这里帮助我并告诉我出了什么问题吗? Here is my plugin https://www.npmjs.com/package/ionic-capacitor-sms-access这是我的插件https://www.npmjs.com/package/ionic-capacitor-sms-access

(Note: only the android folder) (注:仅限android文件夹)

It works, there are two ways to achieve this.它有效,有两种方法可以实现这一点。

Method 1 (from the official tutorial)方法一(来自官方教程)

What you need to do is just to keep following that tutorial to the section of 'Custom JavaScript', where shows demonstrates how to use your plugin within your ionic/typescripts codes.您需要做的只是继续遵循该教程的“自定义 JavaScript”部分,其中演示了如何在您的 ionic/typescripts 代码中使用您的插件。

such as:如:


import { Plugins } from '@capacitor/core';

const { SuperGreatPlugin } = Plugins;

export class CustomSuperPlugin {
  constructor() {
  }
  customAwesomeness() {
    SuperGreatPlugin.awesome();
  }
}

Notes:笔记:

  1. the name of the plugin class, ie 'SuperGreatPlugin' must be same with your java class for the plugin.插件名称 class,即“SuperGreatPlugin”必须与插件的 java class 相同。

Method 2 (javascript way)方法2(javascript方式)

You can also import your plugin from the npm package you published.您还可以从您发布的 npm package 导入您的插件。 But be aware of the first line of the generated definitions.ts, declare module "@capacitor/core" .但请注意生成的定义.ts 的第一行, declare module "@capacitor/core" This means you have to find your plugin from the specific module, here it is 'Plugins' as other plugins usage.这意味着您必须从特定模块中找到您的插件,这里是“插件”作为其他插件的使用。

The following is the method 2:以下是方法二:

import { SuperGreatPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';

async pluginEcho() {
    await Plugins.SuperGreatPlugin.echo({value: 'bla bla bla'})
}

call the function pluginEcho() in your ionic page.在您的离子页面中调用 function pluginEcho()。

When I started I head really trouble distinguishing the web and native plugin but finally I understand that they are completely separated.当我开始时,我真的很难区分 web 和原生插件,但最后我明白它们是完全分开的。

I recommend to rename the exported web plugin object in your web.ts file and add Web in the name.我建议在您的 web.ts 文件中重命名导出的 web 插件 object 并在名称中添加Web For me this was also necessary in order to not have compile errors.对我来说,这也是必要的,以免出现编译错误。

TS2308: Module './definitions' has already exported a member named 'YourPlugin'. Consider explicitly re-exporting to resolve the ambiguity.

If you want to use your web plugin you import and use it like:如果你想使用你的 web 插件,你可以导入并使用它:

import { YourWebPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';

YourWebPlugin.callSomething();

If you want to use the native plugin you import and use it like:如果您想使用您导入的本机插件并像这样使用它:

import { Plugins } from '@capacitor/core';

const { YourPlugin } = Plugins;
YourPlugin.callSomething();

Don't forget to expose your native plugin to your android app project where you use your custom plugin https://capacitor.ionicframework.com/docs/plugins/android#export-to-capacitor不要忘记将您的本机插件公开给您使用自定义插件https://capacitor.ionicframework.com/docs/plugins/android#export-to-capacitor的 android 应用程序项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM