我想在不止一次的同一活动中显示插页式广告.我正在使用以下方法进行插页式广告,但它只显示插页式广告一次并且从不重新加载.
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// for interstitial
interstitial = new InterstitialAd ( this );
interstitial.setAdUnitId (my ad id);
AdRequest.Builder adRequestBuilder=new AdRequest.Builder ();
interstitial.setAdListener ( new AdListener (){
}
});
interstitial.loadAd ( adRequestBuilder.Build ());
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bNext);
gButton = (Button) findViewById(R.id.bPrev);
//Just set one Click listener for the image
iButton.setonClickListener(iButtonChangeImageListener);
gButton.setonClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentimage++;
currentimage = currentimage % images.length;
hImageViewPic.setimageResource(images[currentimage]);
/ / here to show interstitial
if (interstitial. isLoaded ()){
interstitial.show ();
}
} };
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentimage--;
currentimage = (currentimage + images.length) % images.length;
hImageViewPic.setimageResource(images[currentimage]);
}
};
当我按下按钮然后插页式显示.我希望将间隙显示为一些图像.例如,在10日和20日之后或之后或之后5分钟等.请帮助我如何回忆间隙并在一些图像之后显示它.请帮我编写代码或编辑我的代码.我完全不知道如何才能完成这项任务.我会非常感激的.
解决方法:
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}
@Override
public void onAdFailedToLoad(int errorCode) {
long timestamp = System.currentTimeMillis();
if(timestamp > lastTimeAdFail + 120*1000)
{
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Load the interstitial ad again
interstitial.loadAd(adRequest);
}
}
@Override
public void onAdClosed ()
{
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Load the interstitial ad again
interstitial.loadAd(adRequest);
}
});
同时修改你的onclick监听器
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentimage++;
currentimage = currentimage % images.length;
hImageViewPic.setimageResource(images[currentimage]);
if (interstitial.isLoaded()) {
long timestamp = System.currentTimeMillis();
if(timestamp > lastTimeAdShown + 300*1000)
{
interstitial.show();
lastTimeAdShown = timestamp;
}
}
else
{
long timestamp = System.currentTimeMillis();
if(timestamp > lastTimeAdFail + 120*1000)
{
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Load the interstitial ad.
interstitial.loadAd(adRequest);
lastTimeAdFail = timestamp;
}
}
} };
编辑:将两个类变量定义为
private long lastTimeAdShown=System.currentTimeMillis();
private long lastTimeAdFail=System.currentTimeMillis();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。