我怎么能说从科尔多瓦/ PhoneGap的WebView功能,例如本地函数来显示广告。
How can I call a native function from cordova/phonegap webview for example for showing an ad.
编辑:好吧,我FUNALLY得到它,我会写一些步骤,大家谁不知道该怎么做(只是备用2天内,您一生的:D)
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)
A)如果你只有科尔多瓦/ PhoneGap的和,并希望从JS调用做到这一点:
A) if you have just cordova/phonegap and and wish to call from js do this:
1)更换下列code与您现有的DroidGap活动。
1) Replace the following code with your existing DroidGap Activity.
@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)的电流(这个)活动添加自定义函数如下。
2) Add the custom function in current (this) activity as following.
@JavascriptInterface
public void customFunctionCalled() {
Log.e("Custom Function Called", "Custom Function Called");
}
3)现在从你的HTML / JavaScript的code调用这个函数如下。
3) Now call this function from your HTML/JavaScript code as following.
window.MainActivity.customFunctionCalled();
B.1)如果你在一个web视图实现科尔多瓦/ PhoneGap的,并希望调用JS从这样做: (和要调用的正常功能)
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)
1),将其添加到主要的Java文件:
1) Add this to the main java file:
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
2)声明类JavaScriptInterface:
2) Declare the class JavaScriptInterface:
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
@JavascriptInterface
public void showLog(){
Log.v("blah", "blah blah");
}
}
3)JS用`window.JSInterface.showLog()调用它
B.2)如果你有科尔多瓦/ PhoneGap的在web视图实现,并希望从JS调用(和要调用的UI功能,如面包)做到这一点:
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:
1),将其添加到主要的Java文件:
1) Add this to the main java file:
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
2)声明类JavaScriptInterface:
2) Declare the class 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)添加敬酒功能下的:
3) Add the toast function underneath:
public void showToast(){
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
4)`window.JSInterface.myFunction称之为();
4) Call it with `window.JSInterface.myFunction();
正如你看到的,如果你需要使用你需要用你的函数进入activity.runOnUiThread,以便它可以从JS调用用户界面功能。
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.
*如果你想从Java调用jQuery的方法做到这一点:
*If you want to call from java a jquery method do this:
Java的:
cordova_webview.loadUrl("javascript:window.functionn()");
JavaScript的:
Javascript:
window.function = punish;
有一个愉快的一天!
Have a nice day!
在.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());
}
在JavaScript的
In the javascript
window.MainActivity.customFunctionCalled();
这只能在TargetSDK< 17。一个的AndroidManifest.xml targetSDK需要设置&其中; 17.而对于TargetSDK> = 17,我想你需要创建一些自定义的插件。我用这个方法降低我的目标SDK现在。
This will only work on 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. I use this process lowering my target SDK for now.