如何在webview应用上显示非页内广告?

时间:2016-09-15 01:35:19

标签: android android-studio webview admob android-webview

我想在网页视图应用中展示插页式广告,并在打开网页(文章)后立即打开广告,因为admob政策告诉我,广告会在正常转换点中显示。

你能告诉我怎么做吗?

我还想了解如何控制广告外观,因为我不希望每次用户打开链接时都会展示广告?

2 个答案:

答案 0 :(得分:1)

如果你使用像Cordova / Phonegap / Ionic这样的框架,你最好的机会就是使用插件:https://github.com/appfeel/admob-google-cordova/

否则你可以尝试获取插件并删除co​​rdova依赖项,并重构它以使用你的javascript钩子进行调用,但这将是一个痛苦!

答案 1 :(得分:1)

我建议你访问How to call admob Interstitial ad from android webview JavascriptInterface 我已经给出了从android webview中显示admob interstitial的解决方案。

示例:

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