在2个活动之间显示插页式广告

时间:2017-11-03 07:10:36

标签: android interstitial

我有2项活动。其中,活动___有一个按钮,点击该按钮将导航到下一个活动。

单击按钮后,我需要显示非页内广告。

汽车活动 -

    public class car extends AppCompatActivity implements View.OnClickListener {

        private AdView mAdView;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.car);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

            findViewById(R.id.car_btn).setOnClickListener(listener_car_btn);

        }

        View.OnClickListener listener_car_btn = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(car.this, motorcycle.class);

}

摩托车活动 -

public class motorcycle extends AppCompatActivity {

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

现在,如果用户从Car activity导航到Motorcycle活动,他们应该在摩托车活动开始之前看到插页式广告。

如何实现这一目标?

P.S - 我已经在项目中使用了横幅广告。

3 个答案:

答案 0 :(得分:4)

尝试以下代码

 InterstitialAd mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("your unitid");

 AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("Test Device Id")
                .build();

mInterstitialAd.loadAd(adRequest);

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
               gotoNext();
    }
});

View.OnClickListener listener_car_btn = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    gotoNext();
                }

        }
});

private void gotoNext(){
    Intent intent = new Intent(car.this, motorcycle.class);
}

答案 1 :(得分:0)

在按钮上点击展示广告,并在广告关闭活动2的火灾意图时使用AdListener

mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
           // fire intent for second activity
        }

        @Override
        public void onAdOpened() {
            // Code to be executed when the ad is displayed.
        }

        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }

        @Override
        public void onAdClosed() {
            // fire intent for second activity 
        }
    });

答案 2 :(得分:0)

请按照此代码显示两个活动之间的交互。

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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;

public class MainActivity extends AppCompatActivity {

    private Button button1,button2;

    private InterstitialAd mInterstitialAd;

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


        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

        requestNewInterstitial();
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });

        button1=findViewById(R.id.btn1);
        button2=findViewById(R.id.btn2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded() || mInterstitialAd!=null){
                    mInterstitialAd.show();
                }else{
                    startActivity(new Intent(MainActivity.this,SecondActivity.class));
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded() || mInterstitialAd!=null){
                    mInterstitialAd.show();
                }else{
                    startActivity(new Intent(MainActivity.this,SecondActivity.class));
                }
            }
        });
    }

    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mInterstitialAd.loadAd(adRequest);
    }
}