0

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.

        }

    });
}

}
brvnbld
  • 1
  • 1
  • 8
  • You can use [SharedPreferences](https://developer.android.com/training/data-storage/shared-preferences) – Paresh P. May 28 '18 at 13:19

3 Answers3

2

You can use SharedPreferences :

SharedPreferences.Editor editor = getSharedPreferences("PreferencesName", MODE_PRIVATE).edit();
 editor.putInt("myInt", 12345);
 editor.apply();

and retrive it when you want :

SharedPreferences prefs = getSharedPreferences("PreferencesName", MODE_PRIVATE); 
int myInt = prefs.getInt("myInt", 0); // 0 is default
E.Abdel
  • 1,992
  • 1
  • 13
  • 24
  • 1
    This is the best way. SharedPreferences is ideal for storing simple primitives as key value pairs. – Dan 0 May 28 '18 at 14:49
  • 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. – brvnbld May 29 '18 at 06:20
  • Are you sure that you get your value by prefs.getInt("myInt", 0); ? – E.Abdel May 29 '18 at 07:25
  • yeah,bcoz, even after I close the App and reopen it, the value isn't affected. That means the value is returned by the prefs.getInt right? – brvnbld May 29 '18 at 12:34
  • Yes, if you don't change it, it is saved even you quit your app – E.Abdel May 29 '18 at 12:35
  • and if you save you int as "myInt", you have te retrieve it using the same name – E.Abdel May 29 '18 at 12:38
  • But right now, the count is not updated. Each time I close the app and open it again and click the button, the value gets incremented. If I am inside the app and click the the button many times , its not getting updated. – brvnbld May 30 '18 at 15:59
0

You need to save this value in the database and fetch this value when you open it. To send a message, read about alarm manager in the official documentation.

Alarme Manager

Edi
  • 615
  • 5
  • 15
0
  • best way is SharedPreferences and this the best tutorial for use it here

  • to include a message to users to visit the app after 30 minutes to click again

you have multi way

send Notification from the background after 30 min countdown Timer

or by Toast in background and more than this

Community
  • 1
  • 1
Hassan Badawi
  • 302
  • 1
  • 10