SharedPreferences 并检查用户是否登录
SharedPreferences and check if user is logged in
有没有办法检查另一个 class 的 SharedPreferences,并通过直接调用方法在应用程序的所有活动中使用此方法?以及如何在每个 activity 中调用该方法?
a我不确定这是否是您要查找的内容,还有更多方法可以做到这一点。但是如果你这样写你的共享首选项 class:
public class Prefs {
private static final String PREFS = "prefs";
private SharedPreferences prefs;
public Prefs(Context context){
prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}
public void saveMyString(String string) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("key", string).apply(); //The "key" should be stored in a variable in the rel app.
}
public String getMyString(Context context, String WidgetId){
return prefs.getString("key", "");
}
}
然后您在 activity 中获得 Prefs 的实例,并且可以调用您的方法。
有没有办法检查另一个 class 的 SharedPreferences,并通过直接调用方法在应用程序的所有活动中使用此方法?以及如何在每个 activity 中调用该方法?
a我不确定这是否是您要查找的内容,还有更多方法可以做到这一点。但是如果你这样写你的共享首选项 class:
public class Prefs {
private static final String PREFS = "prefs";
private SharedPreferences prefs;
public Prefs(Context context){
prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}
public void saveMyString(String string) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("key", string).apply(); //The "key" should be stored in a variable in the rel app.
}
public String getMyString(Context context, String WidgetId){
return prefs.getString("key", "");
}
}
然后您在 activity 中获得 Prefs 的实例,并且可以调用您的方法。