“SharedPreferences” Ответ

Android Save Int

public void saveInt(int val, String key){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(key, val);
    editor.apply();
}

public int getInt(String key){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
    int val = prefs.getInt(key, 0);
    return val;
}
Colorful Centipede

Android SharedPreferences

SharedPreferences sharedPref = getSharedPreferences("name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", "Value");
editor.commit();
Jesus

Получите значение предпочтения Android

boolean mBoolean = PreferenceManager.getDefaultSharedPreferences(yourContext).getBoolean(key, defaultValue); //getBoolean will return defaultValue is key isn't found
//you can also use getInt, getFloat, getLong, getString, getStringSet and change the variable type, of course
Fedeboss

SharedPreferences

/*============StartActivity.java============*/

public class StartActivity extends AppCompatActivity {

private static final String PREF_LOGIN = "LOGIN_PREF";
private static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);

    Intent intent = null;
    if(preferences.contains(KEY_CREDENTIALS)){              //if user is currently logged in;
        intent = new Intent(this, MainActivity.class);
    }else {                                                 //if user is not yet logged in;
        intent = new Intent(this, LoginActivity.class);
    }
    startActivity(intent);
}

}

/*============LoginActivity.java============*/

public class LoginActivity extends AppCompatActivity {

public static final String PREF_LOGIN = "LOGIN_PREF";
public static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";

private Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //...

    loginButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //call login api...


            //on your sucess callback; we save the credentials...
            /*
            SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
            editor.putString(KEY_CREDENTIALS, "DUMMY CREDENTIALS");
            editor.commit();
            */

            //on your failure callback; we clear the credentials...

            /*
            SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();
            */
        }


    });
}
</ProgAli>

Установить значение предпочтения Android

boolean committed = PreferenceManager.getDefaultSharedPreferences(yourContext).edit().putBoolean(key, mValue).commit();
//you can also use putInt, putFloat, putLong, putString, putStringSet and change the value of mValue
//committed is true if everything went alright, false if there was an error
Fedeboss

Ответы похожие на “SharedPreferences”

Смотреть популярные ответы по языку

Смотреть другие языки программирования