多按钮上的Admob非页内广告点击
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多按钮上的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());
}
});
答案
试试这个方法。
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一起使用它可以在广告关闭时控制流量。
另一答案
您可以使用枚举方法处理多次点击,以便对点击次数加载不同的活动。而且你只需要实现一个插页式添加。您必须声明枚举列表以标识每个活动。
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;
}
以上是关于多按钮上的Admob非页内广告点击的主要内容,如果未能解决你的问题,请参考以下文章