Using Shared Preferences in Android

前端 未结 5 1108
别跟我提以往
别跟我提以往 2021-01-27 05:05

I have three activities, A, B & C. Where A is a splash Activity and B Contains Login screen which consist of user Id and Password Text Field and one button to login. When I

相关标签:
5条回答
  • 2021-01-27 05:30

    Storing username and password is a bad practice instead use JWT. Take a JWT token from your response and then store it in your shared preference. If your API doesn't return any JWT in reply then at least hash your username and password before saving, but it is also unsafe.

    0 讨论(0)
  • 2021-01-27 05:36

    This is relatively easy. You can store the username and password directly in the SharedPreference as follows:

    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    
    p.edit().putString("username", username).commit();
    p.edit().putString("password", password).commit(); //SECURITY HAZARD: read below...
    

    Then you can retrieve it like this:

    String username = p.getString("username", "");
    String password = p.getString("password", "");
    

    The issue when doing this is that the password is available globally. You need to have a way to prevent others from viewing it. The way you do this is by encrypting the password when you save it and decrypting it when you load it using a symmetric key. Here's a tutorial on encryption: http://android.voxisland.com/code_examples/How_to_encrypt_and_decrypt_strings.rhtml

    Let me know if this helps you at all.

    Emmanuel

    0 讨论(0)
  • 2021-01-27 05:39

    to use shared preference in android

    public class SharedPref {
    
    public static void setValue(String key, String value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    public static String getValue(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }
     public static void setAlertDialog(Context mContext,String title,String message)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
    
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
    
    }
    

    and to set and get value from the class use following code

    SharedPref.setConfig("key","value",Context);
    SharedPref.getConfig("key",Context);
    SharedPref.setAlertDialog(Context,"title","Content to print");
    
    0 讨论(0)
  • 2021-01-27 05:41

    Write it from Activity A like this:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
                Editor editor = sp.edit();
                editor.putString("YOUR_KEY", "username");
                editor.commit();
    

    You can read it afterwards with:

    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
            String username = p.getString("YOUR_KEY", null);
    
    0 讨论(0)
  • 2021-01-27 05:44

    This is the best way to use Shared preference just call this method

    Store shared preference

    public static void setDefaults(String key, String value, Context context) {
        SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(context);
    
        SharedPreferences.Editor editor = prefs.edit();
    
        editor.putString(key, value);
    
        editor.commit();
    }
    

    Call this method and pass argument like this

    Classname.setsetDefaults("key","Value",context);
    

    Get Shared Value

        public static String getDefaults(String key, Context context) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getString(key, null);
        }
    

    Call this method And pass key

    ClassName.getDefaults("Key",Context);
    
    0 讨论(0)
提交回复
热议问题