2015-07-03 16 views
-3

我已經使用timer禁用了button 2分鐘。如何使用`SharedPreferences`設置禁用狀態

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。 在此先感謝。

+0

您可以使用該服務。 – Pankaj

回答

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); 
    } 

然後在以往任何時候ü需要啓用或禁用button.U可以在 保存會話存儲。

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