I basically need the count value to be stored somewhere, and get retrieved when the app opens again. Pls suggest an easy way and I also like to include a message to users to visit the app after 30 minutes to click again. I have altered the code per your suggestions, and its working fine, but the values get updated only on exiting and reopening the app. It doesn't update the value in realtime.
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
//SharedPreferences.Editor editor = getSharedPreferences("PreferencesName", MODE_PRIVATE).edit();
private Button watchButton;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final String value = "key";
// int myInt = prefs.getInt("myInt", 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
final int i = sharedpreferences.getInt(value, 0);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-02434841XXXXXXXX/XXXXXXXXXX");
// mInterstitialAd.loadAd(new AdRequest.Builder().build());
watchButton = findViewById(R.id.button_send);
watchButton.setOnClickListener(new View.OnClickListener() {
// Listen for when user presses button
public void onClick(View v) {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
// If a interstitial is ready, show it
if(mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else
{
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
// Otherwise end this activity (go back to first activity)
}
});
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// mInterstitialAd.loadAd(new AdRequest.Builder().build());
int count = i;
count += 1;
TextView tv_data=findViewById(R.id.disCount);
tv_data.setText(Integer.toString(count));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(value, count);
editor.apply();
//setContentView(R.layout.activity_main);
// Load the next interstitial.
}
});
}
}