[英]How to call native function from cordova 3.x
How can I call a native function from cordova/phonegap webview for example for showing an ad. 如何从cordova / phonegap Webview调用本机函数,例如用于展示广告。
EDIT: OK I FUNALLY GOT IT and I'm gonna write some steps for all of you who don't know how to do that (just to spare 2 days of your lifetime :D) 编辑:好的,我最终得到了,我要为不知道该怎么做的所有人写一些步骤(只是为了节省您一生的2天时间:D)
A) if you have just cordova/phonegap and and wish to call from js do this: A)如果您只有cordova / phonegap并希望从js调用,请执行以下操作:
1) Replace the following code with your existing DroidGap Activity. 1)将以下代码替换为现有的DroidGap活动。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init(); // Calling this is necessary to make this work
appView.addJavascriptInterface(this, "MainActivity");
/* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */
super.loadUrl("file:///android_asset/www/index.html");
}
2) Add the custom function in current (this) activity as following. 2)在当前(此)活动中添加自定义函数,如下所示。
@JavascriptInterface
public void customFunctionCalled() {
Log.e("Custom Function Called", "Custom Function Called");
}
3) Now call this function from your HTML/JavaScript code as following. 3)现在,从您的HTML / JavaScript代码中调用此函数,如下所示。
window.MainActivity.customFunctionCalled();
B.1) if you have cordova/phonegap implemented in a webview and wish to call from js do this: (and want to call normal function) B.1)如果您已在webview中实现了cordova / phonegap并希望从js调用,请执行以下操作:(并希望调用普通函数)
1) Add this to the main java file: 1)将其添加到主Java文件中:
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
2) Declare the class JavaScriptInterface: 2)声明类JavaScriptInterface:
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
@JavascriptInterface
public void showLog(){
Log.v("blah", "blah blah");
}
}
3) Call it from js with `window.JSInterface.showLog(); 3)使用`window.JSInterface.showLog();从js调用它;
B.2) if you have cordova/phonegap implemented in a webview and wish to call from js (and want to call UI function, like a toast) do this: B.2)如果您已在webview中实现了cordova / phonegap并希望从js调用(并且想调用UI函数,例如toast),请执行以下操作:
1) Add this to the main java file: 1)将其添加到主Java文件中:
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
2) Declare the class JavaScriptInterface: 2)声明类JavaScriptInterface:
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
@JavascriptInterface
public void myFunction()
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Code that interact with UI
showToast();
}
});
}
}
3) Add the toast function underneath: 3)在下面添加烤面包功能:
public void showToast(){
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
4) Call it with `window.JSInterface.myFunction(); 4)用`window.JSInterface.myFunction();调用它;
As you see if you need a function that uses the UI you need to wrap your function into activity.runOnUiThread so that it can get called from js. 如您所见,如果需要使用UI的函数,则需要将函数包装到activity.runOnUiThread中,以便可以从js中调用它。
*If you want to call from java a jquery method do this: *如果要从Java调用jquery方法,请执行以下操作:
Java: Java:
cordova_webview.loadUrl("javascript:window.functionn()");
Javascript: Javascript:
window.function = punish;
Have a nice day! 祝你今天愉快!
In the .java file 在.java文件中
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init(); // Calling this is necessary to make this work
appView.addJavascriptInterface(this, "MainActivity");
super.loadUrl(Config.getStartUrl());
}
In the javascript 在javascript中
window.MainActivity.customFunctionCalled();
This will only work on TargetSDK<17 . 这仅适用于TargetSDK <17。 An androidManifest.xml targetSDK need to be set < 17. And for TargetSDK >=17 , I guess you'll need to create some custom plugin. 需要将androidManifest.xml targetSDK设置为<17。对于TargetSDK> = 17,我想您需要创建一些自定义插件。 I use this process lowering my target SDK for now. 我现在使用此过程降低目标SDK。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.