-2

I need to generate random int to give device id when my app run for the first time, then save it to SharedPreffs, show id on TextView, and when is turned on again show saved id from SharedPreffs on TextView.

public class MainActivity extends AppCompatActivity {

    public static final String MyPREFERENCES = "MyPrefs";
    public static final String KEY_DEVICE = "id";
    SharedPreferences sharedpreferences;
    String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        deviceid = (TextView) findViewById(R.id.deviceid);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        if (!sharedpreferences.contains(id)) {
            SharedPreferences.Editor editor = sharedpreferences.edit();
            id = String.valueOf(new Random().nextInt(900000) + 100000);
            editor.putString(KEY_DEVICE, id);
            editor.commit();
            deviceid.setText(id);
        }

        deviceid.setText(id);
    }
}

Above code generates random int and show it on TexView, but every time I hide or turn off the app, the device id changes Could you explain me what i have to do to achive my goal.

shmakova
  • 6,076
  • 3
  • 28
  • 44
Kamil Sz
  • 15
  • 6
  • `contains` first parameter is the key, not the value. Your key is `KEY_DEVICE`, why are you checking by `id`? – Vladyslav Matviienko Jul 14 '17 at 10:06
  • https://stackoverflow.com/questions/7496840/get-android-shared-preferences-value-in-activity-normal-class Please go to the link and learn how to get sharedpreferences value.. – wonderPub Jul 14 '17 at 10:10

4 Answers4

2

There should be "id" or KEY_DEVICE

Replace

!sharedpreferences.contains(id)

to

!sharedpreferences.contains(KEY_DEVICE)

Also

deviceid.setText(id);

will show null in that case

So you have to add before setText

id = sharedpreferences.getString(KEY_DEVICE,"0");
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
0

This is because you are just checking if there is no shared preference value then select a random number and add it to device but you are not getting any value in the on create method i.e you are not getting any shared preference value that you stored before. Simply get your stored value and then check if the value is exist, if exists then set in textview otherwise call the condition check. Hope it Helps

0

Put a check for null before fetching the value from sharedPreferences. If it is null : then save the id
else : fetch the older one only.

If this check : if(!sharedpreferences.contains(id)) is required then keep it nested under null check.

Aakanksha
  • 119
  • 1
  • 10
0

Try this

String MyPREFERENCES = "MyPrefs";
        String KEY_DEVICE = "device_id";
        SharedPreferences sharedpreferences;
        SharedPreferences.Editor editor;

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        if (sharedpreferences.contains(KEY_DEVICE)) {
            id = sharedpreferences.getString(KEY_DEVICE, "0");
            deviceid.setText(id);
        } else {
            id = String.valueOf(new Random().nextInt(900000) + 100000);
            editor = sharedpreferences.edit();
            editor.putString(KEY_DEVICE, id);
            editor.apply();
            deviceid.setText(id);
        }
debugger
  • 580
  • 1
  • 6
  • 16