Invalid custom cordova plug-in call

has been studying cordova to package the vue project into webapp,. I want to try to write a cordova plug-in myself, and then use js to call the result of this component execution.

cordova project structure is as follows:

1. Create components ExtraInfo

ExtraInfo.java the contents of the file are as follows:

import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

public class ExtraInfo extends CordovaPlugin {
  
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        Activity activity = this.cordova.getActivity();
        if(action.equals("getExtra")) {
            Intent i = activity.getIntent();
            if(i.hasExtra(Intent.EXTRA_TEXT)) {
                callbackContext.success(i.getStringExtra(Intent.EXTRA_TEXT));
            }else{
                callbackContext.error("");
            }
            return true;
        }
        return false;
    }
}

ExtraInfo.js the contents of the file are as follows:

var exec = require("cordova/exec");
 
exports.getExtra = function(success, error) {
    exec(success, error,"ExtraInfo","getExtra", []);
};

plugin.xml the contents of the file are as follows:

<?xmlversion="1.0"encoding="utf-8"?>
<plugin id="com.zyd.cordova" version="0.0.1"
        xmlns=" http://apache.org/cordova/ns/plugins/1.0"
        xmlns:android=" http://schemas.android.com/apk/res/android">
    <name>ExtraInfo</name>
    <description>Description</description>
    <js-module name="ExtraInfo" src="www/ExtraInfo.js">
        <clobbers target="cordova.plugins.ExtraInfo" />
    </js-module>
    <platform name="android">
        <config-file parent="/*" target="res/xml/config.xml">
            <feature name="ExtraInfo">
                <param name="android-package" value="com.zyd.cordova.ExtraInfo" />
            </feature>
        </config-file>
        <source-file src="src/android/ExtraInfo.java" target-dir="src/com/zyd/cordova" />
    </platform>
</plugin>

2. Add components ExtraInfo to the project

then go to . Execute the command cordova plugin add ExtraInfo under the / platforms/android path, and add the component to the project

3, calling component

for simplicity, I call the cordova plug-in directly from the www/js/index.js project template file created by cordova :

receivedEvent: function(id) {
    var cordova = require("cordova");
    var extraInfo = cordova.require("com.zyd.cordova.ExtraInfo");

    extraInfo.getExtra(function(message) {
        alert("111" + message);
    }, function(message) {
        alert("2222" + message);
    });
}

finally reference the file in www/index.html

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

I don"t know why the result of the call is not displayed all the time. The alert statement of getExtra () in index.js is not executed. What is the problem when it is defined before the sentence that alert can be executed?

Menu