在 Broadcast Receiver 中使用另一个 class 的共享首选项

Use Shared preferences from another class in Broadcast Receiver

我是 android 的新手。我使用的是 SessionHandler class,我将 LoginActivity 中的用户名保存在共享首选项中。我想从广播接收器

的 class 中的共享首选项访问此用户名

这是 SessionHandler 的代码 class。

 public SharedPreferences getLoginPreferences() {
    // This sample app persists the registration ID in shared preferences,
    // but
    // how you store the regID in your app is up to you.
    return context.getSharedPreferences(
            LoginActivity.class.getSimpleName(), Context.MODE_PRIVATE);
}

public void storeLoginSession(String str_Name) {
    final SharedPreferences prefs = getLoginPreferences();
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("name", str_Name);

    editor.commit();
}

我想在startService()这里访问这个名字name。

  public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
               startService();
        }
  }

   private void startService() {

    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
    strDate = sdf.format(c.getTime());
}

我怎样才能得到这个值?我尝试使用上下文但没有工作。谁能帮帮我。

创建一个名为 AppPreference.class class

public class AppPreference {

private SharedPreferences mSharedPreferences;

public static final String KEY_LOGIN= "KEY_LOGIN";


public AppPreference(Context context) {
    super();
    mSharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
}

public void setLoginStatus(String value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putString(KEY_POSTCODE_LOGIN, value);
    editor.commit();
}
public String getLoginStatus() {
    return mSharedPreferences.getString(KEY_POSTCODE_LOGIN,"");
}

}

您可以通过调用

来节省价值
AppPreference appPreference = new AppPreference(context);
appPreference .setLoginStatus("value you want to save");

getvalue 调用

AppPreference appPreference = new AppPreference(context);
appPreference .getLoginStatus();

您好,请使用此示例代码

public class 首选项处理器 { public static boolean storeNameToPrefernce(上下文上下文, 字符串键,字符串 getDetailsS​​tring) {

    // TODO Auto-generated method stub
    SharedPreferences mySharedPreferences;
    try{
        mySharedPreferences=context.getSharedPreferences(MyConstants.PREFERENCE_NAME,0);
        SharedPreferences.Editor editor=mySharedPreferences.edit();
        editor.putString(key,getDetailsString);
        editor.commit();
    }
    catch(Exception e){
        System.out.println(e);
        return false;
    }
    return true;
}
public static String getNameFromPreference(Context context,String key)
{
    String value;
    SharedPreferences mySharedPreferences;
    try{
        mySharedPreferences = context.getSharedPreferences(MyConstants.PREFERENCE_NAME,0);
        value = mySharedPreferences.getString(key, "NONE");
    }catch(Exception e){
        return "NONE";
    }
    return value;
}

}

从首选项中获取字符串 class:

在示例活动中 class。

字符串名称=PreferenceHandler.getNameFromPreference(sampleActivity.this,key);

您可以为 sharedPreferences 创建全局函数:

public static void setSharedPrefString(Context context, String key, String value) {

    SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preference.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getSharedPrefString(Context context, String key) {

    SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
    if (preference != null) {
        return preference.getString(key, "");
    }
     return "";
}

从 BroadcastReceiver 的 onReceive 调用 "getSharedPrefString"。 如果您觉得有用,请告诉我。

使用 SharedPreference 的最佳方式是将您的自定义首选项 class 包裹起来,例如:

public class YourPreference {   
    private static YourPreference yourPreference;
    private SharedPreferences sharedPreferences;

    public static YourPreference getInstance(Context context) {
        if (yourPreference == null) {
            yourPreference = new YourPreference(context);
        }
        return yourPreference;
    }

    private YourPreference(Context context) {
        sharedPreferences = context.getSharedPreferences("YourCustomNamedPreference",Context.MODE_PRIVATE);
    }

    public void storeLoginSession(String str_Name) {
        SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
        editor.putString("name", str_Name);
        prefsEditor.commit();           
    }
 }

然后您可以使用

从任何 class 可用的内容中获取 YourPrefrence 实例
YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.storeLoginSession(YOUR_STRING);

我这样做很简单:

主要class:

public class SWP extends Activity implements LocationListener
{   ....
    public static final String MY_PREFERENCES = "SWP_Preferences";
    public static SharedPreferences preferencesSwp;
    public static SharedPreferences.Editor preferencesEditorSwp;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {    ....
         preferencesSwp = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
         preferencesEditorSwp = preferencesSwp.edit();
         ....
    }
    ....
}

在任何其他方面,阅读

value1 = SWP.preferencesSwp.getString("PREF_value1", "init_text");

并写

SWP.preferencesEditorSwp.putString("PREF_value1", value1 );
SWP.preferencesEditorSwp.commit();