Admob:每次展示的插页式广告 [英] Admob: Interstitial ads showing every time
问题描述
我正在完成视频应用程序,并且在退出视频活动时正在显示插页式广告。我只想每X分钟显示一次,但似乎每次离开屏幕时都会显示一次。
i'm finishing a video application and i'm displaying interstitial ads when leaving the video activity. I just want to show it once every X minutes, but it seems to be showing every time i leave that screen.
这是我的活动代码。
OnCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
videoId = b.getString("videoId");
setContentView(R.layout.youtube_player);
interstitialAd= new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
etc...
OnBackPressed:
OnBackPressed:
@Override
public void onBackPressed() {
ShowAds();
}
private void ShowAds() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
}else{
super.onBackPressed();
}
}`
当然,在AdMob中这样设置:
of course, it is setted like this into AdMob:
注意:我的应用程序尚未发布,因此显示预览或样本。我正在使用广告单元ID:
Note: my application is not published, so it is showing the "preview" o "sample". I am using my ad unit ID:
谢谢,
推荐答案
两个选项:
-
插页式广告仅在我离开当前活动X分钟后才显示。
Interstitial ads show when I am leaving current Activity but only If I completed X minute on current Activity.
boolean isAdShow=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int minute=1; // X minute
isAdShow=false;
new CountDownTimer(minute*60000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
isAdShow=true;
}
}.start();
}
private void ShowAds() {
if (interstitialAd.isLoaded() && isAdShow) {
interstitialAd.show();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
}else{
super.onBackPressed();
}
}
不要等待用户按下后退按钮并保留当前的活动,只需从Timer的 onFinish()
方法调用 ShowAds()
。
我建议使用1 st ,因为它也不会违反AdMob广告政策和用户体验。
I'll recommend to use 1st one because it not violate AdMob Ad policy and user experience too.
编辑
您也可以使用X次计数器,例如 X = 3
,即在3次调用 onCreate()
方法之后,才有资格展示广告。
You can also use X times counter, like when X = 3
i.e After 3 times call of onCreate()
method make eligible to show Ad.
public static int adCounter;
public static final int TIME_COUNTER=3; //Should be always greater than zero
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adCounter++;
}
private void ShowAds() {
if (interstitialAd.isLoaded() && adCounter%TIME_COUNTER==0) {
interstitialAd.show();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
}else{
super.onBackPressed();
}
}
这篇关于Admob:每次展示的插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!