【问题标题】:How to Referesh value I am getting from SharedPreference on start of Activity如何刷新我在活动开始时从共享偏好中获得的价值
【发布时间】:2020-08-12 06:22:11
【问题描述】:

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

1) how to delete sharedpreferences ,Quit and launch application from first actvity in android

2) clear the value of sharedpreferences

3)Remove Shared preferences key/value pairs

4) SharedPreferences Clear/Save

在将数据写入 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,那么我正在使用这个值来更改通知图标.

有通知时:

没有通知时:

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

【问题讨论】:

  • 在您的 mainactivity 的 onCreate 中再次将首选项值设置为 0
  • 我试过了,但这不起作用,在达到 SharedPreferene getInt();值已更改并保持不变...

标签: android sharedpreferences


【解决方案1】:

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

public  void clearSpecificKey(String key){

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

注意事项: 您应该创建一个通用的 Shared Preference 类,如下所示

公共类 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"

你可以在这里阅读更多关于它Best Practices

【讨论】:

  • 非常感谢,不过SharedPrefs里面的代码我没看懂,有些代码片段不知道...报错。
  • 必须是 masterKeyAlias 它需要一个依赖更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多