onPageFinished不适用于广告

时间:2014-08-22 09:57:43

标签: webview interstitial

我正在尝试在我的webview应用的某些网址中显示插页式广告,包括onPagefinished和url.contains,但它不起作用。这是我的代码:

@SuppressLint(“SetJavaScriptEnabled”)公共类Juego扩展ActionBarActivity {

  /** ID intersticial */
  private InterstitialAd interstitialAd;      
  private static final String AD_UNIT_ID = "ca-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_juego);


    //Página web enlazada:
    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.loadUrl("http://xxxxxxxxxxxxxxxxx.php");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.setHorizontalScrollBarEnabled(false);
            //Barra de progreso
    final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setCancelable(false);

    myWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressDialog.setTitle("Cargando");
            progressDialog.setMessage("Por favor espera");
            progressDialog.show();
            progressDialog.setProgress(0);
            activity.setProgress(progress * 1);

            progressDialog.incrementProgressBy(progress);

            if(progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }
    });
  }

public void onPageFinished (WebView view, String url) {
     // Anuncios intersticiales: Create the interstitial.
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    // Create ad request.
    AdRequest adRequest = new AdRequest.Builder().build();

    // Begin loading your interstitial.
    interstitialAd.loadAd(adRequest);
    if (url.contains("game")) {
        interstitialAd.show();
    }
}

}

任何帮助?

1 个答案:

答案 0 :(得分:0)

使用此

public void onPageFinished (WebView view, String url) {

interstitial = new InterstitialAd(this);

        interstitial.setAdUnitId("ca-app-pub-5236990339136194/xxxxxxxxxx");



        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);


    interstitial.setAdListener(new AdListener(){
        public void onAdLoaded(){
             displayInterstitial();


    }
      // Invoke displayInterstitial() when you are ready to display an interstitial.
      public void displayInterstitial() {
        if (interstitial.isLoaded()) {
          interstitial.show();
        }
   }

    } );  
}

并且不要忘记在onCreate博客上添加此内容

AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);