使用Broadcast Receiver中其他类的共享首选项

时间:2015-09-23 05:10:08

标签: android broadcastreceiver sharedpreferences

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

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

以下是SessionHandler类的代码。

 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()中访问此名称。

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

我怎样才能获得这个价值?我尝试使用上下文但没有工作。任何人都可以帮助我。

5 个答案:

答案 0 :(得分:5)

使用SharedPreference的最佳方法是在自定义Preference类中包装,如:

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

然后,您可以使用

从任何可获得contet的类中获取YourPrefrence实例
YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.storeLoginSession(YOUR_STRING);

答案 1 :(得分:1)

您可以为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”。 如果您发现它有用,请告诉我。

答案 2 :(得分:0)

创建一个名为AppPreference.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 call

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

答案 3 :(得分:0)

您好,请使用此示例代码

公共类PreferenceHandler { public static boolean storeNameToPrefernce(Context context,             String key,String 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;
}

}

从Preference类获取字符串:

在SampleActvity类中。

String name = PreferenceHandler.getNameFromPreference(sampleActivity.this,key);

答案 4 :(得分:0)

我这样做的方式很简单:

主要课程:

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