【问题标题】:Load interstitial admob advert on 3rd start of an activity在活动第 3 次开始时加载插页式 admob 广告
【发布时间】:2016-07-12 10:00:15
【问题描述】:

public class Starting extends ActionBarActivity {

    private final String PREFERENCE_NAME = "ad_counter_preference"; //class level variable
    private final String COUNTER_INTERSTITIAL_ADS = "ad_counter"; //class level variable
    private int mAdCounter = 0; //class level variable

    //adview
    private AdView mAdView;

    //interstitial
    private String TAG = Starting.class.getSimpleName();
    InterstitialAd mInterstitialAd;



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

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        //interstitial
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));


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

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });

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

        editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
        editor.commit();

        mAdCounter = preferences.getInt(COUNTER_INTERSTITIAL_ADS, 0);

        if (mAdCounter == 3) {
            // Load interstitial ad now
         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 showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }


//banner ads
    @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();
    }
}

我正准备启动我的应用程序,但希望它是正确的,并且不会因为插页式广告到处弹出而惹恼用户,所以我想控制它们何时加载!在一项活动中,我希望在活动开始的第 3 次加载插页式广告!到目前为止,我已经让广告在活动开始时加载,但现在它每次都开始。

public class Starting extends ActionBarActivity {

    //adview
    private AdView mAdView;

    //interstitial
    private String TAG = Starting.class.getSimpleName();
    InterstitialAd mInterstitialAd;


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

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        //interstitial
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

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

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });
    }
    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }


//banner ads
    @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();
    }
}

【问题讨论】:

    标签: java android android-studio admob interstitial


    【解决方案1】:

    您可以在 android 中使用 SharedPreferences。 每当用户打开特定活动时,递增存储在用作计数器的 sharedpreferences 中的字段。 同时,随时检查计数器。当它达到三个时,加载插页式广告。

    您可以参考以下代码:

    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();
    }}
    

    【讨论】:

    • 试过了,但活动开始时广告仍在加载,我在 if 语句中的 counter == 3 之后放置了 showInterstital() 方法,但仍会立即加载!也许我错过了一些东西或者我应该删除我的部分代码?如果你能更新我发布的 Java,那可能会更容易!
    • 我添加了带有共享首选项的新 java 代码
    • 您已经在 setAdListener 内的 onCreate 中调用 showInterstitial 方法。这就是为什么广告一直出现的原因。请删除该代码。并将其放在 mAdCounter == 3 的 if 条件检查中。
    • 我应该将所有广告代码放在 if 条件中,还是应该将所有内容包装到一个方法中并在 if 条件下调用该方法?很混乱,因为以前从未处理过广告
    • 我也用横幅广告更新了答案。检查代码。
    【解决方案2】:

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

    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() 方法在 displayTimes = 0 之后显示广告;我需要把所有这些都放在创建中吗?
    • 我认为 onCreate 将是正确的地方。我刚刚编辑了我的答案以检查条件 displayTimes == 3 而不是 0,因为我注意到在 0 上索引总是会被跳过
    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多