【问题标题】:How to create cordova plugin for ionic如何为离子创建科尔多瓦插件
【发布时间】:2016-04-01 12:48:22
【问题描述】:

我是插件创建新手,我搜索了如何创建 cordova 插件,我得到了这个链接,https://blogs.oracle.com/mobile/entry/introduction_to_custom_cordova_plugin 通过点击此链接,我创建了一个警报插件,但我无法在 ionic 中使用它,这是放置在 src/android 文件夹下的 Alert.java 的代码。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaArgs;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class Alert extends CordovaPlugin {
 @Override
ublic boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) 
      throws JSONException {

    if (action.equals("alert")) {
      alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
      return true;
    }
    return false;
  }

  private synchronized void alert(final String title, 
                                  final String message, 
                                  final String buttonLabel, 
                                  final CallbackContext callbackContext) {
    new AlertDialog.Builder(cordova.getActivity())
    .setTitle(title)
    .setMessage(message)
    .setCancelable(false)
    .setNeutralButton(buttonLabel, new AlertDialog.OnClickListener() {
      public void onClick(DialogInterface dialogInterface, int which) {
        dialogInterface.dismiss();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
      }
    })
    .create()
    .show();
  }
} 

这是放在 www 文件夹下的 alert.js 代码

  module.exports = {

      alert: function(title, message, buttonLabel, successCallback) {
        cordova.exec(successCallback,null,"Alert","alert",[title, message, buttonLabel]);
      }

};

plugin.xml 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        id="cordova-plugin-my-alert"
        version="0.0.2">

  <name>Alert</name>
  <description>A Cordova plugin that displays an alert popup dialog</description>
  <license>Apache 2.0</license>

  <js-module src="www/Alert.js" name="Alert">
    <clobbers target="Alert" />
  </js-module>

  <!-- android -->
  <platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
      <feature name="Alert">
        <param name="android-package" value="com.acme.plugin.alert.Alert" />
      </feature>
    </config-file>
    <source-file src="src/android/Alert.java" target-dir="src/com/acme/plugin/alert" />
  </platform>
</plugin>

请帮助我哪里出错了。

【问题讨论】:

    标签: android cordova ionic-framework


    【解决方案1】:

    我已经通过在警报函数中添加失败回调而不是 null 来解决上述错误,并且我错过了 Alert.java 类中的包名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      相关资源
      最近更新 更多