插页式广告不重复
interstitial ads not repeating
我在一段时间后无法显示插页式广告。我的代码只显示一次(60 秒后)。我想每 60 秒显示一次插页式广告。我知道以这种方式实施广告不是一个好主意,但我需要这个。我的代码如下:
package com.ronie.admobads;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.mInterstitialAd.show();
}
}, 60000);
}
});
}
}
@Abhishek,我试过你的方法,但插页式广告没有显示。我只能看到横幅广告。你能检查一下我的代码中的任何地方是否有误吗?
完整代码如下:
package com.ronie.admobads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
mInterstitialAd.show();
createRequest();
}
}, 1,1, TimeUnit.MINUTES);
}
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
您可以这样安排您的活动:
createRequest();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if(mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
}, 1, 1, TimeUnit.MINUTES);
和createRequest
方法
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
编辑
我的错误,我很抱歉,线程因 java.lang.IllegalStateException
而死,需要在主 UI 线程上调用。
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
});
}
}, 1, 1, TimeUnit.MINUTES);
我在一段时间后无法显示插页式广告。我的代码只显示一次(60 秒后)。我想每 60 秒显示一次插页式广告。我知道以这种方式实施广告不是一个好主意,但我需要这个。我的代码如下:
package com.ronie.admobads;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.mInterstitialAd.show();
}
}, 60000);
}
});
}
}
@Abhishek,我试过你的方法,但插页式广告没有显示。我只能看到横幅广告。你能检查一下我的代码中的任何地方是否有误吗? 完整代码如下:
package com.ronie.admobads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
mInterstitialAd.show();
createRequest();
}
}, 1,1, TimeUnit.MINUTES);
}
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
您可以这样安排您的活动:
createRequest();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
@Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if(mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
}, 1, 1, TimeUnit.MINUTES);
和createRequest
方法
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
编辑
我的错误,我很抱歉,线程因 java.lang.IllegalStateException
而死,需要在主 UI 线程上调用。
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
});
}
}, 1, 1, TimeUnit.MINUTES);