Hello everyone!
I’ve recently received help here and had pleasing results; I have one last question and I should finally be good to go!
I have recently posted this on stackoverflow, but results have been stagnant.
Basically, I have an app that displays an interstitial after a certain number of actions (button clicks) on a few pages of my app. I am afraid that a user may wander around my app and have a whole bunch of interstitials smacked in their face.
So, I am trying to limit the number of interstitial ads per session.
And my plan of action is to use SharedPrerences to tell android that I do not want more than 2 interstitial ads to pop up per session (unless there are better ideas out there?)
My code is below:
The problem with the code is that when using the SharedPreferences, no interstitial ads are showing up. The logcat doesn’t tell me that my interstitials are loading or even failing to load. Almost like they are nonexistent.
However without the SharedPreferences my interstitials load perfectly.
Without the SharedPreferences, I’m telling android that I would like to have a banner ad loaded every time and that I would like to load an interstitial once every 5 times. I hope that using the SharedPreferences will make the app only deliver two interstitials to load per session for this app.
Thank you very much in advance!
package com.xxxxx.xxxxxxx;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.xxxxx.xxxxxxx.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Commonproblemsmenuclass extends Activity{
private InterstitialAd interstitial;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.commonproblemsmenu);
//Prepare the Interstitial Ad
interstitial = new InterstitialAd(Commonproblemsmenuclass.this);
//Insert the Ad Unit ID
interstitial.setAdUnitId("xxxxxxxxxxxxxx/xxxxxxxxxxxxxx");
//Locate the Banner Ad in xml
AdView adView = (AdView) this.findViewById(R.id.adView);
//Request for Ads
AdRequest adRequest = new AdRequest.Builder()
//Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("xxxxxxxxxxxxxx")
.build ();
//Load ads into Banner Ads
adView.loadAd(adRequest);
//Load ads into Interstitial Ads
loadInterstitial();
//setting up buttons
Button cp1 = (Button) findViewById(R.id.commonproblems1);
Button cp2 = (Button) findViewById(R.id.commonproblems2);
Button cp3 = (Button) findViewById(R.id.commonproblems3);
Button cp4 = (Button) findViewById(R.id.commonproblems4);
Button cp5 = (Button) findViewById(R.id.commonproblems5);
// On Click Listener and counters
cp1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Commonproblemsmenuclass.this, Commonproblem1class.class));
counter++;
displayInterstitial();
}
});
cp2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Commonproblemsmenuclass.this, Commonproblem2class.class));
counter++;
displayInterstitial();
}
});
cp3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Commonproblemsmenuclass.this, Commonproblem3class.class));
counter++;
displayInterstitial();
}
});
cp4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Commonproblemsmenuclass.this, Commonproblem4class.class));
counter++;
displayInterstitial();
}
});
cp5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Commonproblemsmenuclass.this, Commonproblem5class.class));
counter++;
displayInterstitial();
}
});
}
//SHAREDPREFERENCES INITIATED HERE
private void displayInterstitial(){
//sharedpreferences
SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
if(sp.getInt("key", 0) < 5){ // the zero is the default value if it's empty, the if statement checks if your sp integer is less than 5.
// and continues if it checks true, if not, skip the whole statement, that's the logic here
Editor ed = sp.edit();
ed.putInt("key",sp.getInt("key", 0) +1);
ed.commit();
//do ads work
if (interstitial.isLoaded() && counter < 5){ // that's your counter, the && checks if your first argument is true before it moves on to the
//second argument.. if it checks false, all is false & checks
// checks both arguments regardless of their outcome
interstitial.show();
loadInterstitial();
}
}
//end shared preferences, end of if-statement, end of logic code
} // end of method
private void loadInterstitial()
{
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("xxxxxxxxxxxxxx")
.build();
interstitial.loadAd(adRequest);
}
}