0
我正面臨從webview調用android Admob插頁式廣告的問題。我無法得到任何適當的解決方案。最後,我想出瞭解決方案來調用JavaScript的插頁式廣告。看看下面的答案。如何從android webview調用admob插頁式廣告JavascriptInterface
我正面臨從webview調用android Admob插頁式廣告的問題。我無法得到任何適當的解決方案。最後,我想出瞭解決方案來調用JavaScript的插頁式廣告。看看下面的答案。如何從android webview調用admob插頁式廣告JavascriptInterface
上述問題的解決方案
導入所有需要的軟件包。
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調用呼叫:
@Override
public void onAdLoaded(){
displayLoadedAd();
}
您需要將插頁式廣告的數量降低到你的優先。在你的admob控制檯中。
是的,我們顯示添加沒有從js調用。但我的要求是當某個按鈕被按下時顯示廣告。 –