How to show up interstrial ads, while opening two

2019-07-26 21:46发布

So i am using intent to load two URL's from single web view with the help of switch case. Now i want to implement, intrestrial ads to show when anyone clicks on button before webview is loaded.

Below is my code. """Home Activity""

public class home extends AppCompatActivity {
    AdView mAdview3;
    private InterstitialAd interstitialAd;
    public RatingBar ratingbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ratingbar = (RatingBar)findViewById(R.id.ratingBar);
        Button button = (Button) findViewById(R.id.button3) ;

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");    
        mAdview3 = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
        mAdview3.loadAd(adRequest);

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        AdRequest adRequest1 =new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
        interstitialAd.loadAd(adRequest1);

    }


public void click(View view) 
{
    Intent intent = new Intent(home.this, playerzpot_team.class);
    if (interstitialAd.isLoaded()) 
    {
        interstitialAd.show();
        interstitialAd.loadAd(new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build());
    } 
    else 
    {
        switch (view.getId()) 
        {
            case R.id.button3:
                intent.putExtra("url", "http://teams.com/");
                startActivity(intent);
                break;

            case R.id.button2:
                intent.putExtra("url", "https://nikhil.wordpress.org");
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}

}

In home.xml file. I have created 2 buttons with id button 2 and button 3, both having onclick = "click" . Clicking on this buttons will open two different buttons in webview.

Second activity. xml is having a normal just a simple webview.

And here is the second activity.java file

public class playerzpot_team extends AppCompatActivity {
private WebView aboutus_myWebView;
AdView mAdview5;
String url;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playerzpot_team);
    String url = getIntent().getStringExtra("url");
    aboutus_myWebView = (WebView) findViewById(R.id.playerzpot_webview);

    aboutus_myWebView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = aboutus_myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    aboutus_myWebView.loadUrl(url);

    MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");       
    mAdview5 = (AdView) findViewById(R.id.adView2);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
    mAdview5.loadAd(adRequest);

}

@Override
public void onBackPressed() 
{
    if (aboutus_myWebView.canGoBack()) 
    {
        aboutus_myWebView.goBack();
        aboutus_myWebView.goBack();
    } 
    else 
    {
        super.onBackPressed();
    }
}

}

At present both buttons are performing their neccesary task, I need help in showing up interistial ads before webview gets open.

Can anyone help in modifying this code. Help will be appreciated.

Thanks in advance.

0条回答
登录 后发表回答