Phonegap插件不能调用android的本地函数

Phonegap plugin cant call native function of android

本文关键字:函数 android 插件 不能 调用 Phonegap      更新时间:2023-09-26

我是phonegap和android的新手。

我在phonegap和android中创建了一个插件,使用javascript调用原生函数。

我的代码如下。

<标题>插件/BannerLink.js h1> 的html视图文件
function bannerPressed(link){
    alert(link.rel);
    //window.location.href=link.rel;
    //window.open(link.rel);
    BannerLink.callNativeFunction( nativePluginResultHandler,nativePluginErrorHandler,link.rel );
}
function nativePluginResultHandler (result) {
    alert("SUCCESS: 'r'n"+result );
}
function nativePluginErrorHandler (error) {
    alert("ERROR: 'r'n"+error );
}

我的BannerLink.java文件

package org.apache.cordova.example;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.app.AlertDialog;
import android.util.Log;
@SuppressWarnings("deprecation")
public class BannerLink extends Plugin {
    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId)  {
        // TODO Auto-generated method stub
        AlertDialog alertDialog=new AlertDialog.Builder(null).create();
        alertDialog.setTitle("Reset...");
        alertDialog.show();
        Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!"); 
        //only perform the action if it is the one that should be invoked 
        return new PluginResult(PluginResult.Status.ERROR);
    }
}

我的config.xml文件

<plugin name="BannerLink" value="org.apache.cordova.example.BannerLink"/>

我正在使用phonegap 2.0

请纠正我的错误

有几个错误是你的JS。"cordova"是小写而不是大写,你只需要在exec方法中提供插件名而不是完整路径:

var BannerLink = {
    callNativeFunction: function (success, fail, resultType) {
        return    cordova.exec(success, fail,
                    "BannerLink", 
                    "nativeFunction", 
                    [resultType]);
    }
};

您试图在Java代码中显示的AlertDialog需要包装在可运行文件中,例如:

    Runnable runnable = new Runnable() {
        public void run() {
            AlertDialog alertDialog=new AlertDialog.Builder(null).create();
            alertDialog.setTitle("Reset...");
            alertDialog.show();
        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);