我试图调用" displayAd()"功能从我的JavaScript。这是我的代码示例:
MyActivity.java:
public class MyActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Inject WebAppInterface methods into Web page by having Interface name 'Android'
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
mWebView.loadUrl("http://myWebSite.com");
}
public class WebAppInterface{
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void doSomething1(){
…
}
@JavascriptInterface
public void doSomething2(){
…
}
}
我如何使用此课程或其他方式调用我的功能来显示广告,就像我现在正在使用其他功能一样。这似乎很容易,但就像我是一个初学者,我不知道该怎么做。这是我如何称呼他们的另一个例子:
function example1(){
Android.doSomething1();
}
function example2(){
Android.doSomething2();
}
最后这里是文档的例子:
import com.google.android.gms.ads.*;
public class BannerExample extends Activity {
private InterstitialAd interstitial;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}