[英]Phonegap plugin cant call native function of android
I am new on phonegap and android. 我是phonegap和android上的新手。
I created a plugin in phonegap and android to call native function using javascrip. 我在phonegap和android中创建了一个插件,以使用javascrip调用本机函数。
My coode is as below. 我的表象如下。
var BannerLink = {
callNativeFunction: function (success, fail, resultType) {
//alert(resultType);
return Cordova.exec( success, fail,
"org.apache.cordova.example.BannerLink",
"nativeFunction",
[resultType]);
alert(resultType);
}
};
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 );
}
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);
}
}
<plugin name="BannerLink" value="org.apache.cordova.example.BannerLink"/>
I am using phonegap 2.0 我正在使用phonegap 2.0
Please correct me where I have made mistake. 请改正我犯错的地方。
There are a few errors is you JS. 有一些错误是您JS。 "cordova" is lower case not upper case and you just need to provide the plugin name not the full path in the exec method: “ cordova”是小写而不是大写,您只需要在exec方法中提供插件名称而不是完整路径即可:
var BannerLink = {
callNativeFunction: function (success, fail, resultType) {
return cordova.exec(success, fail,
"BannerLink",
"nativeFunction",
[resultType]);
}
};
The AlertDialog you are trying to show in the Java code needs to be wrapped in a runnable for instance: 您试图在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.