简体   繁体   中英

sharedpreferences

hi i hope someone can help i am new to developing, i have managed to get this app i am building running previously on a windows machine and i am redevloping for android. the scenario is this. I need a user to input data, i can get this into shared prefences which is ok. But i need this to be a 1 time operation, the data is an id number. first time the application is run the user enters their Id number, the application uses this id number as part of an email which is sent. The next time the user runs the app i dont want the app asking them to enter their id again.

I got this working on a windows based machine by writing the data out to a file, when the app started it would check if file existed, if file existed then it would open the relevant form for email, if the file didn't exist it would open the data input form.

I hope i have explained the issue clearly enough. all help appreciated thanks.

What you are looking for is persistent storage in android. There are four ways to store the ID of the user

  1. Shared Preferences
  2. Internal Storage
  3. External Storage
  4. SQLite Database
  5. Network Connection

Since you have mentioned that you have already done this on windows by storing it on file you can do the same here with external storage method .

You can find more about these here

Use internal storage:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

I don't seem to understand the problem

            SharedPreferences sharedPreference= PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = sharedPreference.edit();
            editor.putString("KEY-REQUIRED", VALUE);
            editor.commit();

Well notice KEY-REQUIRED this is what you call when you want the data of VALUE.

This KEY-REQUIRED is completely rewritable you can overwrite it non-stop and the value can keep changing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM