Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在活动第三次开始时加载间隙admob广告_Java_Android_Android Studio_Admob_Interstitial - Fatal编程技术网

Java 在活动第三次开始时加载间隙admob广告

Java 在活动第三次开始时加载间隙admob广告,java,android,android-studio,admob,interstitial,Java,Android,Android Studio,Admob,Interstitial,公共类启动扩展了ActionBarActivity{ 私有最终字符串首选项\u NAME=“ad\u counter\u PREFERENCE”//类级别变量 私有最终字符串计数器\u interstitual\u ADS=“ad\u COUNTER”;//类级别变量 private int mAdCounter=0;//类级别变量 //忠告 私人顾问马德维尤; //间隙的 私有字符串标记=start.class.getSimpleName(); 间质间隙; @凌驾 创建时受保护的void(Bu

公共类启动扩展了ActionBarActivity{
私有最终字符串首选项\u NAME=“ad\u counter\u PREFERENCE”//类级别变量
私有最终字符串计数器\u interstitual\u ADS=“ad\u COUNTER”;//类级别变量
private int mAdCounter=0;//类级别变量
//忠告
私人顾问马德维尤;
//间隙的
私有字符串标记=start.class.getSimpleName();
间质间隙;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_启动);
mAdView=(AdView)findviewbyd(R.id.AdView);
AdRequest AdRequest=新建AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
//间隙的
Minterstitalad=新的间隙(本);
Minterstitalad=新的间隙(本);
//设置ad单元ID
minterstitalad.setAdUnitId(getString(R.string.interstitual_full_screen));
//将广告加载到间隙广告中
Minterstitalad.loadAd(地址);
minterstitalad.setAdListener(新的AdListener(){
已加载的公共无效(){
showInterstitial();
}
});
SharedReferences preferences=GetSharedReferences(PREFERENCE\u NAME,Context.MODE\u PRIVATE);
SharedReferences.Editor=首选项.edit();
编辑:putInt(反插播广告,mAdCounter);
commit();
mAdCounter=preferences.getInt(计数器间隙ADS,0);
如果(mAdCounter==3){
//立即加载间隙ad
showInterstitial();
mAdCounter=0;//清除计数器变量
}否则{
mAdCounter++;//递增计数器变量
}
//将计数器值保存回SharedReferences
编辑:putInt(反插播广告,mAdCounter);
commit();
}
私人间隙{
if(minterstitalad.isload()){
minterstitalad.show();
}
}
//横幅广告
@凌驾
公共无效暂停(){
if(mAdView!=null){
mAdView.pause();
}
super.onPause();
}
@凌驾
恢复时公开作废(){
super.onResume();
if(mAdView!=null){
mAdView.resume();
}
}
@凌驾
公共空间{
if(mAdView!=null){
mAdView.destroy();
}
super.ondestory();
}

}
您可以在android中使用SharedReferences。 每当用户打开特定活动时,递增存储在SharedReferences中的字段,该字段用作计数器。 同时,随时检查柜台。当它达到3时,加载间隙ad

您可以参考以下代码:

public class Starting extends ActionBarActivity {

private final String PREFERENCE_NAME = "ad_counter_preference";
private final String COUNTER_INTERSTITIAL_ADS = "ad_counter";
private int mAdCounter = 0;

private InterstitialAd mInterstitialAd;
private AdRequest mInterstitialAdRequest;
private AdRequest mBannerAdRequest;
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    loadInterstitialAd();
    loadBannerAd();

    SharedPreferences preferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    mAdCounter = preferences.getInt(COUNTER_INTERSTITIAL_ADS, 0);

    if (mAdCounter == 3) {
        // Load interstitial ad now
        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });
        mAdCounter = 0; //Clear counter variable
    } else {
        mAdCounter++; // Increment counter variable
    }

    // Save counter value back to SharedPreferences
    editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
    editor.commit();
}

private void loadInterstitialAd() {
    mInterstitialAdRequest = new AdRequest.Builder()
            .build();

    //interstitial
    mInterstitialAd = new InterstitialAd(this);

    // set the ad unit ID
    mInterstitialAd.setAdUnitId("Your Ad unit Id");

    // Load ads into Interstitial Ads
    mInterstitialAd.loadAd(mInterstitialAdRequest);
}

private void loadBannerAd() {
    mAdView = (AdView) findViewById(R.id.adView);
    mBannerAdRequest = new AdRequest.Builder()
            .build();
    mAdView.loadAd(mBannerAdRequest);
}

private void showInterstitial() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}

@Override
public void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}}

我建议使用SharedReference。每次活动开始时都输入以下代码

SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();

int displayTimes = preferences.getInt("kDisplayTimes", 0); 

if (displayTimes == 3) {
    // Shown 3 times, reset counter
    displayTimes = 0;

    // Show interstitial
}
else {
    // Less than 3 times, increase counter
    displayTimes++;
}

// Save counter back to SharedPreferences
editor.putInt("kDisplayTimes", displayTimes);
editor.commit();

我明白了,所以我调用showInterstital()方法在displaysTimes=0之后显示广告;我需要把所有这些都放在onCreate中吗?我认为onCreate是正确的地方。我刚刚编辑了我的答案来检查条件
displayTimes==3
,而不是0,因为我注意到在0上索引总是会被跳过,但在活动开始时ad仍在加载,我在if语句中将showInterstital()方法放在计数器==3之后,但仍然会立即加载!我遗漏了什么,或者我应该删除部分代码吗?如果你能更新我发布的java,那可能会更容易!我添加了带有共享首选项的新java代码,您已经在setAdListener内的onCreate中调用了ShowInterstitual方法。这就是为什么广告总是出现的原因。请删除该代码。然后将它放在mAdCounter==3的if条件检查中。我应该将所有的ad代码放在if条件中,还是应该将所有内容包装到一个方法中并在if条件中调用该方法?因为我以前从未处理过广告,所以我也用横幅广告更新了答案。检查代码。