如何使用`SharedPreferences`设置禁用状态

时间:2015-07-03 06:10:34

标签: android sharedpreferences

我已使用button停用timer 2分钟。

 btn_Verify.setEnabled(false);
 Log.e("LoginActivity", "counter :" + counter);
 Handler h = new Handler();
 h.postDelayed(new Runnable() {
     @Override
     public void run() {
        btn_Verify.setEnabled(true);
     }
  },120000);

如果我退出应用并重新开始,则会启用button。因此,我必须将其保存在SharedPreference中并将其阻止2分钟,然后取消阻止它。 任何人都可以告诉我如何在SharedPreference中保存它。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

这是会话商店类。

public class SessionStore {

    public static boolean button_value = true;

    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                STOREPREFF_NAME, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean getBoolean(Context context, String key,
            boolean defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                STOREPREFF_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defaultValue);
    }
  

然后你需要启用或禁用按钮.U可以保存它   在会话商店。

For saving
 SessionStore.saveBoolean(getApplicationContext(),
                SessionStore.button_value , "true or false");
For getting 
 SessionStore.getBoolean(getApplicationContext(),
                SessionStore.button_value , "");
相关问题