在按钮上点击Admob非页内广告 [英] Admob Interstitial ad on Button Click
问题描述
我有一个应用程序,并且正在使用admob标语.现在,我想在按钮"点击上展示插页式广告.我的应用程序有2个活动,我想在第二个活动中展示插页式广告.第二个活动有一个按钮,该按钮可返回第一个活动,并且我想在单击按钮后显示广告.我可以通过单击按钮来显示广告,但是当用户关闭广告或按返回按钮时,它仍然保留在第二个活动中,这就是我面临的问题.
I have an application and I am using admob banners to it. Now I want to show interstitial ad on Button click. My application have 2 activity, and I want to show interstitial ad on the second activity. Second activity has a button which goes back to first activity, and I want to show the ad after the button click. I am able to show the ad on button click but when the user closes the ad or press the back button it remain on the second activity and thats the problem I am facing.
第二活动代码:-
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (interstitial.isLoaded()) {
interstitial.show();
}
else{
Intent i = new Intent();
i.setClass(SecondActivity.this, FirstActivity.class);
startActivity(i);
finish();
}
}
});
}
我希望用户单击按钮时应该看到广告,而当他关闭广告时应该返回到第一个活动
I want when the user clicks on button the ad should be seen and when he closes the ad he should return to first activity
推荐答案
您需要的是为 interstitial
使用 onAdClosed()
方法来确定用户何时关闭广告,以便您可以将其发送回您的第一个活动.
What you need is a AdListener
for your interstitial
that utilizes the onAdClosed()
method to identify when the user closes the ad, so you can send them back to your first activity.
您的非页内广告
设置:
// Create ad request
adRequest = new AdRequest.Builder().build();
// Attempt loading ad for interstitial
interstitial.loadAd(adRequest);
// Create and set AdListener for interstitial
interstitial.setAdListener(new AdListener() {
// Listen for when user closes ad
public void onAdClosed() {
// When user closes ad end this activity (go back to first activity)
finish();
}
});
以及您的 btn
(按钮)设置:
And your btn
(button) setup:
// Create and set OnClickListener for button
btn.setOnClickListener(new OnClickListener() {
// Listen for when user presses button
public void onClick(View v) {
// If a interstitial is ready, show it
if(interstitial.isLoaded()) {
interstitial.show();
}
// Otherwise end this activity (go back to first activity)
else {
finish();
}
}
});
这样,如果用户单击按钮,它将要么:
This way if the user clicks the button it will either:
-
已准备好广告并显示.等待用户关闭广告.
Have an ad ready and display it. Wait for user to close ad.
活动将通过 onAdClosed
-方法关闭.
Activity will then be closed through onAdClosed
-method.
还没有准备好广告.
通过 onClick
方法立即关闭活动.
Instantly close activity through the onClick
-method.
这篇关于在按钮上点击Admob非页内广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!