我想多次在相同的活动中显示插页式广告。我正在使用以下方法进行插页式广告,但它只显示间隙时间一次并且从不重新加载。
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分钟等。请帮助我如何回忆间隙并在一些图像之后显示它。请帮我编写代码或编辑我的代码。我完全不知道如何才能完成这项任务。我会非常感激的。
答案 0 :(得分:12)
将您的adlistener代码修改为
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();