[英]Phonegap plugin cant call native function of android
我是phonegap和android上的新手。
我在phonegap和android中創建了一個插件,以使用javascrip調用本機函數。
我的表象如下。
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"/>
我正在使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.