How to call admob Interstitial ad from android webview JavascriptInterface

旧时模样 提交于 2019-12-18 07:22:56

问题


I was facing problem for calling android Admob interstitial from webview. I could not get any proper solution. Finally I figured out the solution to call the interstitial for javascript. Look into the below answer.


回答1:


Solution for the above problem
Import all the required packages.

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);
    }
}

calling from javascript

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



回答2:


I used this without the javascript call like this:

@Override
            public void onAdLoaded(){
                displayLoadedAd();

            }

You would need to lower the number of interstitial ads to your pref. in your admob console.



来源:https://stackoverflow.com/questions/45714382/how-to-call-admob-interstitial-ad-from-android-webview-javascriptinterface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!