gpt4 book ai didi

javascript - 如何从android webview JavascriptInterface调用admob插页式广告

转载 作者:行者123 更新时间:2023-12-03 03:39:10 25 4
gpt4 key购买 nike

我在从 webview 调用 android Admob interstitial 时遇到问题。我无法得到任何适当的解决方案。最后我找到了调用 javascript 插页式广告的解决方案。查看下面的答案。

最佳答案

上述问题的解决方案
导入所有必需的包。

public class MainActivity extends AppCompatActivity {
WebView myWevView;
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);

myWevView =(WebView)findViewById(R.id.myWevView);
WebSettings ws= myWevView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setDomStorageEnabled(true);
myWevView.getSettings().setUseWideViewPort(true);
myWevView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWevView.setWebViewClient(new WebViewClient());
myWevView.setWebChromeClient(new WebChromeClient());
myWevView.getSettings().setBuiltInZoomControls(true);
myWevView.loadUrl("file:///android_asset/index.html");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener(){
@Override
public void onAdLoaded(){

}
@Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});

}




public void displayLoadedAd(){
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
else
Toast.makeText(getApplicationContext(), "Ad not loded", Toast.LENGTH_SHORT).show();
}

});


}


public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void showAdFromJs(){

Toast.makeText(mContext, "Loading Ad", Toast.LENGTH_SHORT).show();
displayLoadedAd();
}
}
//Controlling navigation
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWevView.canGoBack()) {
myWevView.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

从 JavaScript 调用

$("#btn1").click(function(){
Android.showAdFromJs();
});

关于javascript - 如何从android webview JavascriptInterface调用admob插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com