使用cordova的android世界插件

时间:2014-11-30 09:56:47

标签: javascript android cordova ionic-framework cordova-plugins

我正在尝试使用Cordova为Android构建一个hello world插件。项目运行没有错误但我无法得到结果。我的logcat返回错误Error adding plugin。这是我的代码

javacript

window.func = function(str,callback){
            alert("Outside Call Working");
            cordova.exec(callback, function(err){alert(err)},"HelloPlugin","echo", [str]);
        }
        function callPlugin(str){
            alert("JS Working");
            window.func(str,function(){
                alert("Done!");
            });
        } 

的Android

public class HelloPlugin extends CordovaPlugin{

     @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
            if(action.equals("echo")){
                String message = args.getString(0); 
                callbackContext.success(message);
                return true;
            }
            callbackContext.success(action);
            return true;
        }     
}

config.xml中

<feature name="HelloPlugin">
        <param name="android-package" value="com.ionicframework.myapp554544" />
    </feature>

注意:

上述代码从(Creating a helloWorld plugin for Android using Cordova and Eclipse)此问题中被盗

修改

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>


    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>


    <!-- plugin bridge js -->
    <script src="js/plugin.js"></script>
  </head>
  <body > 
      <a onclick="callPlugin('Plugin Working!')">Click me</a>   
  </body>
</html>

plugin.js

window.func = function(str,callback){
            alert("Outside Call Working");
            cordova.exec(callback, function(err){alert(err)},"org.apache.cordova.plugin.HelloPlugin","echo", [str]);
        }
        function callPlugin(str){
            alert("JS Working");
            window.func(str,function(){
                alert("Done!");
            });
        } 

HelloPlugin.java

package com.ionicframework.myapp554544;

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

public class HelloPlugin extends CordovaPlugin{

     @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
            if(action.equals("echo")){
                String message = args.getString(0); 
                callbackContext.success(message);
                return true;
            }
            callbackContext.success(action);
            return true;
        }     
}

config.xml中

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.ionicframework.myapp554544" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <preference name="loglevel" value="DEBUG" />
    <feature name="Keyboard">
        <param name="android-package" value="com.ionic.keyboard.IonicKeyboard" />
        <param name="onload" value="true" />
    </feature>
    <feature name="Device">
        <param name="android-package" value="org.apache.cordova.device.Device" />
    </feature>
<!--  Here is the plugin entry -->
    <plugin name="HelloPlugin" value="com.example.plugintest.HelloPlugin" />
<!-- end of plugin entry -->
    <name>myApp</name>
    <description>
        An Ionic Framework and Cordova project.
    </description>
    <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="webviewbounce" value="false" />
    <preference name="UIWebViewBounce" value="false" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="BackupWebStorage" value="none" />
</widget>

1 个答案:

答案 0 :(得分:1)

cordova.exec(callback, function(err){alert(err)},"HelloPlugin","echo", [str]);

应更改为:

cordova.exec(callback, function(err){alert(err)},"org.apache.cordova.plugin.HelloPlugin","echo", [str]);

将您的xml条目更改为:

<plugin name="HelloPlugin" value="com.example.plugintest.HelloPlugin" />