sintetico82
sintetico82

Reputation: 503

PreferenceActivity save as private values?

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

Upvotes: 0

Views: 500

Answers (1)

hovanessyan
hovanessyan

Reputation: 31463

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.

Upvotes: 1

Related Questions