如何在活动开始时刷新我从SharedPreference获得的值。

问题描述 投票:0回答:1

我想清除我从SharedPreference得到的数据,我尝试了以下答案,但没有完成我的任务。

1) 如何删除sharedpreferences,退出并启动应用程序从第一行为重力在Android的

2) 清除共享偏好的值

3) 移除共享的首选项键值对

4) SharedPreferences ClearSave

他们都是在将数据写入SharedPreference后,将数据删除,比如editor.remove和.clear......

我在Notification Activity中把数据写入SharedPreference,像这样。

public static final String PREFS_NAME = "com.example.sociapp";

NotificationAdapter notificationAdapter1 = new NotificationAdapter(NotificationsActivity.this, NotificationList, NKeyList);
                    RnotificationList.setAdapter(notificationAdapter1);
                    isthereItem = notificationAdapter1.getItemCount();
                    Toast.makeText(NotificationsActivity.this, ""+isthereItem, Toast.LENGTH_SHORT).show();

                    //writing data into SharedPreference
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putInt("changingicon",isthereItem);
                     //editor.commit();
                    editor.clear();
                    editor.apply();

然后我在MainActivity中得到了这个int值,像这样:

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

 // Reading from SharedPreferen  
try {
//all I want to refresh below line everytime I start MainActivity.
      int ChangeIcon = settings.getInt("changingicon", 0);
      if (ChangeIcon == 0)
      {
          int valuebecomes = 0;
          notificationIconSetting(valuebecomes);
      }
      else
          {
              int valuebecomes = 1;
              notificationIconSetting(valuebecomes);
          }

      Toast.makeText(MainActivity.this, ""+ChangeIcon, Toast.LENGTH_SHORT).show();

  }
  catch (ClassCastException e){e.printStackTrace();}

当我从SharedPreference中得到int值时,我调用的方法是:

 private void notificationIconSetting(int IconTochange)
    {
       if (IconTochange == 0) {

           navigationView.getMenu().getItem(2).setIcon(R.drawable.notification);
       }
       else
           {
               navigationView.getMenu().getItem(2).setIcon(R.drawable.notificationalert);
               navigationView.setItemIconTintList(null);

           }
    }

事实上,当适配器中有通知时,我得到的是一个大于0的int值, 当适配器中没有通知时,int值等于0,然后我就用这个值来改变通知图标。

当有通知的时候。

screenshot

当没有通知的时候

screenshot

现在的问题是,每当我得到一个值,它就会保持不变,直到我清除应用缓存或卸载,然后再重新安装。 我只想在每次启动MainActivity时刷新SharedPreference值。

android sharedpreferences
1个回答
0
投票

你想从共享首选项中删除一个键值,这是我的做法。

public  void clearSpecificKey(String key){

    sharedPreferences.edit().remove(key).apply();
}

需要注意的是:你应该创建一个通用的共享首选项类,像下面这样

public class SharedPrefs {

private static final String MY_PREFS_NAME = "YOUR-PREFERENCE-NAME";

private static SharedPreferences sharedPreferences;
private String masterKeyAlias;
public SharedPrefs(Context context) {

    {
        try {
            masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
        } catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
        }
    }
    try {
        sharedPreferences = EncryptedSharedPreferences.create(MY_PREFS_NAME,masterKeyAlias,context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

}

public  String getStrings(Context mContext, String key){
    return sharedPreferences.getString(key, null);
}

public  void putString(Context mContext, String key, String value ){
    sharedPreferences.edit().putString(key, value).apply();
}

public  boolean getBoolean(Context mContext, String key){
    return sharedPreferences.getBoolean(key, false);
}

public  void putBoolean(Context mContext, String key, boolean value ){
    sharedPreferences.edit().putBoolean(key, value).apply();
}


public static void clear(Context mContext){
  sharedPreferences.edit().remove("user").apply();
}

public  void clearSpecificKey(String key){
    sharedPreferences.edit().remove(key).apply();

}

}

下面是使用方法

声明 :

SharedPrefs sharedPrefs;

初始化。

sharedPrefs = new SharedPrefs(context);

只需调用你想用来在共享偏好中存储值的方法,如

sharedPrefs.putString(context,key,value)

masterKeyAlias是为了保护我的共享偏好。

添加这个你的应用程序gradle

implementation "androidx.security:security-crypto:1.0.0-beta01"

你可以在这里阅读更多关于它 最佳做法

© www.soinside.com 2019 - 2024. All rights reserved.