Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用广播接收器中其他类的共享首选项_Android_Broadcastreceiver_Sharedpreferences - Fatal编程技术网

Android 使用广播接收器中其他类的共享首选项

Android 使用广播接收器中其他类的共享首选项,android,broadcastreceiver,sharedpreferences,Android,Broadcastreceiver,Sharedpreferences,我是android新手。我正在使用一个SessionHandler类,在该类中,我从共享首选项中的LoginActivity保存用户名。我想从广播接收器类中的共享首选项访问此用户名 下面是SessionHandler类的代码 public SharedPreferences getLoginPreferences() { // This sample app persists the registration ID in shared preferences, // but

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


我怎样才能得到这个值?我试着使用上下文,但不起作用。任何人都可以帮助我。

创建一个名为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调用

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

您好,请使用此示例代码

公共类优先处理程序 { 公共静态布尔StoreNameTopReference(上下文, 字符串键,字符串getDetailsString){

}

正在从首选项类获取字符串:

在抽样课上


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

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

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 "";
}
从BroadcastReceive的onReceive调用“getSharedPrefString”。
如果您觉得它有用,请告诉我。

使用SharedReference的最佳方法是将其包装在自定义首选项类中,如:

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可用的任何类中获取PreFrence实例

YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.storeLoginSession(YOUR_STRING);

我这样做很简单:

主要类别:

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

如果您使用了上下文,则看不出这是一种完美的方式,但我建议使用apply()而不是commit(),请参见此处了解原因:它异步运行,这很好!
value1 = SWP.preferencesSwp.getString("PREF_value1", "init_text");
SWP.preferencesEditorSwp.putString("PREF_value1", value1 );
SWP.preferencesEditorSwp.commit();