如何在每加载15页的网页上展示插页式广告?

时间:2019-04-29 04:15:46

标签: java android ads

我正在制作一个Android应用,其中使用Web视图加载URL。我需要在每加载15页后展示插页式广告。我为此做了很多尝试,但我无法做到这一点,希望您能帮助我找到一种解决方法?

我正在附加activity(xml)和MainActivity(java)文件。

请给我建议一种方法吗?

activity(xml):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@android:color/transparent"
        android:id="@+id/frame"
        >
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:layout_gravity="top"
            android:layout_marginTop="-3dp"
            style="?android:attr/progressBarStyleHorizontal"
            android:progress="20"
            android:background="@android:color/transparent"
            android:id="@+id/progressbar"
            />

    </FrameLayout>
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<WebView
    android:layout_below="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webpage"


    />
</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

主要活动:

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;


public class Home extends AppCompatActivity {
    private WebView myWebView;
    private ProgressBar progressBar;
    private FrameLayout frameLayout;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private Runnable runnable;
    private Handler handler;
    private AlertDialog.Builder builder5;
    private AlertDialog alertDialog5;
    private AdView adView;
    private SharedPreferences.Editor editor;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private boolean dobflag = false;
    private InterstitialAd interstitialAd, interstitialAd2;
    private int i = 0;

    SwipeRefreshLayout swipe;

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

        swipe=(SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {


                loadWeb();

            }
        });

        loadWeb();


        MobileAds.initialize(this, getString(R.string.app_id));




        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.interstitial));
        loadInterAd();
        interstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdClosed() {

                loadInterAd();
            }
        });


        //interstitial ads2
        interstitialAd2 = new InterstitialAd(this);
        interstitialAd2.setAdUnitId(getString(R.string.interstitial));
        interstitialAd2.loadAd(new AdRequest.Builder().build());
        interstitialAd2.setAdListener(new AdListener(){
            @Override
            public void onAdClosed() {

                System.exit(0);
            }
        });


        builder5 = new AlertDialog.Builder(this);
        builder5.setCancelable(false);
        builder5.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder5.setMessage("No Internet Connection...");
        alertDialog5 = builder5.create();

        handler = new Handler();
        cycle();







        frameLayout = findViewById(R.id.frame);
        progressBar = findViewById(R.id.progressbar);

        progressBar.setMax(100);
        progressBar.setProgress(0);
    }


    public void loadWeb(){
        String url = "https://www.fantareal.it/";

        myWebView = (WebView)findViewById(R.id.webpage);




        myWebView.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView webView,String url) {
                webView.loadUrl(url);
                frameLayout.setVisibility(View.VISIBLE);
                return true;
            }

        });


        myWebView.setWebChromeClient(new WebChromeClient(){



            public void onProgressChanged(WebView webView,int progress) {
                frameLayout.setVisibility(View.VISIBLE);
                progressBar.setProgress(progress);
                setTitle("Loading...");
                if(progress == 100) {

                    frameLayout.setVisibility(View.GONE);
                    setTitle(webView.getTitle());
                    i++;
                    if ((i%5 == 0) && interstitialAd.isLoaded()) {
                        interstitialAd.show();
                    }


                }
                super.onProgressChanged(webView,progress);
            }


        });
        myWebView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                swipe.setRefreshing(false);

                interstitialAd.show();

            }
        });




        WebSettings webSettings = myWebView.getSettings();
        myWebView.setInitialScale(1);

        webSettings.setDomStorageEnabled(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setAllowFileAccess(true);

        myWebView.getSettings().setSupportZoom(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setDisplayZoomControls(true);
        myWebView.getSettings().setAllowContentAccess(true);
        myWebView.setScrollbarFadingEnabled(false);
        myWebView.loadUrl(url);
        myWebView.setScrollbarFadingEnabled(false);

        //end
    }


private void loadInterAd() {
        if (!interstitialAd.isLoaded()) {
            interstitialAd.loadAd(new AdRequest.Builder().build());
        }

    }

    @Override
    public void onBackPressed() {
        if(myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            AlertDialog.Builder alert = new AlertDialog.Builder(Home.this);
            alert.setMessage("Are you sure you want to exit?");
            alert.setTitle("Exit");
            alert.setIcon(R.mipmap.ic_launcher);
            alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
if (interstitialAd2.isLoaded()) {
    interstitialAd2.show();
}
else
    System.exit(0);

                }
            });
            alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            alert.show();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {

            return true;


        }


        return super.onOptionsItemSelected(item);
    }
    public boolean onKeyDown(int key, KeyEvent keyEvent) {
if(key == KeyEvent.KEYCODE_BACK) {
    if(myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
}
return super.onKeyDown(key,keyEvent);
    }
    public void cycle() {
        if(checkConnection(this)) {
            alertDialog5.cancel();
        }
        else{
            alertDialog5.cancel();
            alertDialog5.show();

        }
        handler.postDelayed(runnable,3000);
    }
    public void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);

    }
    public static boolean checkConnection(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

        if (activeNetworkInfo != null) { // connected to the internet
            // Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show();

            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                return true;
            } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        myWebView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        myWebView.onResume();
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用onUrlChange方法将计数保持为15,请参考以下代码

int count=0; //declare global variable     

WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");            }
    };
    myWebView.setWebViewClient(mWebViewClient);

}
@JavascriptInterface
public void onUrlChange(String url) {
   count=count+1;
  if(count>=15)
   {
     //add code to show the adds
   }
}