0

I am using a library to rate my app and there is a function called .setInstallDays(n) that is called OnCreate method and it recieves a number as argument so that a rate dialog will be shown after the n days of installs. It is working ok, but I would want to set that n number as a random number in a range of numbers. The problem is that I have thought that maybe if it is generated OnCreate method it could generate one different number so that the n will change everytime the Activity is created. How could I generate only one random number so that it is only generated once so that I can use that random number in the .setInstallDays(n) function?

This is the library I am using:AndroidRateHotchemi

This is the code that I have already:


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


        AppRate.with(this)
                .setInstallDays(4) // default 10, 0 means install day.
                .monitor();

        AppRate.showRateDialogIfMeetsConditions(this);


}
  • You only want to generate one random number to share between all activities? – Louis Wasserman Sep 12 '22 at 19:10
  • Well I don´t really know if that could be the solution. I have the `.setInstallDays(n) ` function that is in `OnCreate` method and it is working okay when it recieves always the same number(imagine that it recieves 4 as in the example, so the rate dialog is shown correctly in 4 days). So maybe generating a random number outside so that the same number is always set as argument in the `.setInstallDays(n) ` function of as a no random number, could solve my problem – Eldestornillador Sep 12 '22 at 19:19

2 Answers2

0

You could possible add an override to the onCreate method where you pass in a random number generated outside. eg.

 @Override
    protected void onCreate(Bundle savedInstanceState, int randomDays) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);


        AppRate.with(this)
                .setInstallDays(4) // default 10, 0 means install day.
                .monitor();

        AppRate.showRateDialogIfMeetsConditions(this);


}

Without much further information it's hard to say whether that would fit your needs.

Other that that you could try creating a globally randomized number and toss that in there. Again without much else to go on, its hard to find an adequate solution.

Nate Baker
  • 17
  • 4
0

If you want to generate a random number only once that is persistent across launches of your app, you can save it to SharedPreferences and then check for the saved value before re-generating it. This way it is only generated once, and is the same for all subsequent launches of the Activity and app.

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

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int rand;
    if( prefs.contains("MYRAND")) {
        rand = prefs.getInt("MYRAND", -1);
    }
    else {
        rand = new Random().nextInt(10)+4; // number from 4 to 13
        prefs.edit().putInt("MYRAND", rand).apply();
    }
    
    // do something with rand
    AppRate.with(this)
           .setInstallDays(rand)
           .monitor();
}

You can use nextInt(int bound) on the Java Random class to generate a random integer that is >= 0 and less than bound.

Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • If I have understood it correctly, to generate a number from 3 to 6 for example, this should be the code `rand = new Random().nextInt(4)+3; ` I mean, the number that is outside the brackets is the minimum and the maximum is the one that is inside the brackets + the one that is outside -1, is that right? – Eldestornillador Sep 13 '22 at 19:44
  • `x = nextInt(4)` will produce a number `x` that is `0 <= x < 4` (so 0, 1, 2, or 3 - four total possible numbers). If you add 3 to that, then it would be from 3 to 6 (3, 4, 5, or 6) – Tyler V Sep 13 '22 at 21:52
  • Very good explanation, I have understood it. And out of curiosity, in this line, `rand = prefs.getInt("MYRAND", -1);` what is that -1 used for? I mean, what is the use of the second argument in that function? – Eldestornillador Sep 14 '22 at 19:40
  • That is the default that is used if the requested value isn't present, but since I already check that the value is there in the prior line (`if(prefs.contains("MYRAND"))`) it does nothing in this case since the value will always be present when it calls `getInt`. You can read the [docs](https://developer.android.com/reference/android/content/SharedPreferences#getInt(java.lang.String,%20int)) for that sort of question too... – Tyler V Sep 14 '22 at 20:31
  • I understand, that is exactly the solution I wanted, and the code is working perfectly, but I have just seen that PreferenceManager id deprecated, could you help me to use the non-deprecated substitute method please? – Eldestornillador Sep 14 '22 at 21:32
  • 1. it's not deprecated, you just have to import the [correct one](https://stackoverflow.com/questions/56833657/preferencemanager-getdefaultsharedpreferences-deprecated-in-android-q) (`androidx.preference.PreferenceManager`), 2. did you even read the SharedPreference documentation? You can just do `SharedPreferences prefs = getSharedPreferences("MYPREFS", Context.MODE_PRIVATE);` if you want to not use the PreferenceManager (just pass in whatever name you want). But **please** learn to read the docs - this is super basic stuff. – Tyler V Sep 14 '22 at 22:04
  • You can also read their guide [here](https://developer.android.com/training/data-storage/shared-preferences) on how to use SharedPreferences – Tyler V Sep 14 '22 at 22:16