多按钮上的Admob非页内广告点击

时间:2018-01-27 06:01:22

标签: android admob

我有一个包含10个按钮的活动,每个按钮都会进行不同的活动。当我在按钮点击时添加插页式广告时,它可以正常工作,但是当用户关闭广告时,它总是会让他进行活动(topic_ten)。无论他点击什么按钮,它总是需要他进行活动(topic_ten)。

我想要的是,当用户点击活动按钮(topic_one)时,它会将他带到活动(topic_one)等等。

private InterstitialAd interstitialAd;


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

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-1164579427694627/2090370128");
    interstitialAd.loadAd(new AdRequest.Builder().build());



    bt2 = (Button) findViewById(R.id.button2);

    bt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (interstitialAd.isLoaded()){

                interstitialAd.show();
            }

            else {

                startActivity(new Intent(topics_list.this, topic_one.class));

            }
        }
    });
    interstitialAd.setAdListener(new AdListener(){

        @Override
        public void onAdClosed() {
            startActivity(new Intent(topics_list.this, topic_two.class));
            interstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });
    bt3 = (Button) findViewById(R.id.button3);

    bt3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (interstitialAd.isLoaded()){

                interstitialAd.show();
            }

            else {

                startActivity(new Intent(topics_list.this, topic_two.class));

            }
        }
    });
    interstitialAd.setAdListener(new AdListener(){

        @Override
        public void onAdClosed() {
            startActivity(new Intent(topics_list.this, topic_three.class));
            interstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

2 个答案:

答案 0 :(得分:0)

尝试这种方法。

interstitialAd1 = new InterstitialAd(this);
interstitialAd1.setAdUnitId("ca-app-pub-1164579427694627/2090370128");
interstitialAd1.loadAd(new AdRequest.Builder().build());

interstitialAd2 = new InterstitialAd(this);
interstitialAd2.setAdUnitId("ca-app-pub-1164579427694627/2090370128");
interstitialAd2.loadAd(new AdRequest.Builder().build());


bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) 
    {

        if (interstitialAd1.isLoaded())
            {

                interstitialAd1.show();
            }

        else 
            {

                startActivity(new Intent(topics_list.this, topic_one.class));

            }
        }
    });

interstitialAd1.setAdListener(new AdListener(){

    @Override
    public void onAdClosed() {
        interstitialAd1.loadAd(new AdRequest.Builder().build());
        startActivity(new Intent(topics_list.this, topic_one.class));

    }

});


bt2 = (Button) findViewById(R.id.button1);
bt2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if (interstitialAd2.isLoaded()){

            interstitialAd2.show();
        }

        else {

            startActivity(new Intent(topics_list.this, topic_two.class));

        }
    }
});
interstitialAd2.setAdListener(new AdListener(){

    @Override
    public void onAdClosed() {
        interstitialAd2.loadAd(new AdRequest.Builder().build());
        startActivity(new Intent(topics_list.this, topic_two.class));

    }

});

我刚创建了两个具有相同广告单元ID的插页式广告。与setonclicklistener和adlistener一起使用它可以在广告关闭时控制流量。

答案 1 :(得分:0)

您可以使用枚举方法处理多次点击,以加载不同的点击活动。您只需要实现一个插页式广告即可。您必须声明枚举列表以标识每个活动。

private InterstitialAd mInterstitialAd;    
ActivityType activityType;
public enum ActivityType {
    DRINKLOG, DRINKREPORT,WEIGHTREPORT
}

当实现按钮的点击侦听器时,您只需检查是否已加载add。

fabDrink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                activityType = ActivityType.DRINKLOG;
                mInterstitialAd.show();
                Timber.e("Loading" + "second");
            } else {
                Intent intent = new Intent(MainActivity.this, YourActivity1.class);
                startActivity(intent);
            }
        }
    });

要初始化非页内广告,请添加一个具有其键和侦听器的方法。

mInterstitialAd = newInterstitial();
private InterstitialAd newInterstitial() {
    InterstitialAd interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_id));
    interstitialAd.loadAd(new AdRequest.Builder().build());


    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            mInterstitialAd = newInterstitial();
            switch (activityType) {
                case DRINKLOG:
                    Intent intent = new Intent(MainActivity.this, YourActivity1.class);
                    startActivity(intent);
                    break;

                case DRINKREPORT:
                    Intent activity2= new Intent(MainActivity.this, YourActivity2.class);
                    startActivity(activity2);
                    break;

                case WEIGHTREPORT:
                    Intent activity3= new Intent(MainActivity.this, YourActivity3.class);
                    startActivity(activity3);
                    break;

            }
        }
    });
    return interstitialAd;
}