0

I don't understand the concect of shared preference in android. Shared prefrerence is accessbile by the ather application on the smartphone? If i use this class for save preference:

    public class ImpostazioniActivity extends PreferenceActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.impostazioni);

    } }

Anda after in other activity i use:

  SharedPreferences preference =  PreferenceManager.getDefaultSharedPreferences(getContext());
    if(preference.getString("username","").length() == 0 || preference.getString("password","").length() == 0)
        return  false;
    else
        return  true;

It's ok? I'm sure thet the information are accesible only in my application? Thanks

sintetico82
  • 483
  • 1
  • 9
  • 24
  • You're usage is fine. Any activity that extends from `PreferenceActivity` will use the application's defaultSharedPreferences file. – dmon Nov 13 '11 at 14:03
  • Thank you dmon! Other application in the phone can see my preference? – sintetico82 Nov 13 '11 at 16:33
  • Nope, but they are just plain text files that you can easily read when plugging the phone in. Any important data should be encrypted as @hovanessyan suggests. – dmon Nov 14 '11 at 00:05

1 Answers1

1

Quote:

To get a SharedPreferences object for your application, use one of two methods:

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name

So you should use getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE) where PREFS_FILENAME is the file you want you application to use and Context.MODE_PRIVATE is a file creation mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

EDIT: I looked at your code, if you intent to store any sensitive user infromation (like password) you must encrypt it first! Take a look at this ObscuredSharedPreferences implementation. It will enable you to encrypt/decrypt your data in SharedPreferences.

Community
  • 1
  • 1
hovanessyan
  • 30,580
  • 6
  • 55
  • 83