我正在开发一个Android应用程序,用户可以使用Facebook共享功能。我有Facebook用户的登录详细信息,现在我要做的是这些登录详细信息应该保存,直到应用程序没有被删除。
当我试图找到答案时,我得到一些答案,说明共享偏好。但我并没有清楚它是如何工作的。
这是我想要做的,
Username = username.getText().toString();
PassWord = password.getText().toString();
其中用户名和密码是2个edittext字段。当用户第一次进入Facebook时,我应该将这些数据保存到某个地方以备将来参考,这样他就不需要再次登录了。
任何人都可以告诉我如何实现这个目标吗?
答案 0 :(得分:2)
// Save your info
SharedPreferences settings = getSharedPreferences("my_file_name", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
// Obtain your info
SharedPreferences settings = getSharedPreferences("my_file_name", 0);
String username = settings.getString("username", "");
String password = settings.getString("password", "");
将在以下位置创建文件:
data/data/[your.package.path]/shared_prefs/[your.package.path]_preferences.xml
答案 1 :(得分:2)
您可以使用sharedPreference
。
许多应用程序可能提供一种捕获用户对特定应用程序或活动设置的首选项的方法。为了支持这一点,Android提供了一组简单的API。
首选项通常是名称值对。它们可以作为“共享首选项”存储在应用程序中的各种活动中(注意,它不能跨进程共享)。或者它可能是需要存储特定于活动的东西。
Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.
Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.
共享偏好设置:
在Context类的getSharedPreferences方法的帮助下管理共享首选项。首选项存储在默认文件(1)中,或者您可以指定用于引用首选项的文件名(2)。
(1)以下是指定文件名
时获取实例的方法public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
MODE_PRIVATE是首选项的操作模式。它是默认模式,表示只有调用的应用程序才能访问创建的文件。支持的其他两种模式是MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。在MODE_WORLD_READABLE中,其他应用程序可以读取创建的文件但不能修改它。在MODE_WORLD_WRITEABLE的情况下,其他应用程序也对创建的文件具有写权限。
(2)建议的方法是使用默认模式,而不指定文件名
SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);
最后,一旦有了首选项实例,以下是如何从首选项中检索存储的值:
int storedPreference = preferences.getInt("storedInt", 0);
要在首选项文件中存储值,必须使用SharedPreference.Editor对象。 Editor是SharedPreference类的嵌套接口。
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
编辑器还支持remove()和clear()等方法从文件中删除首选项值。
活动首选项:
共享首选项可供其他应用程序组件使用。但是,如果您不需要与其他组件共享首选项,并希望拥有私人首选项活动。您可以在活动的getPreferences()方法的帮助下完成此操作。 getPreference方法使用getSharedPreferences()方法和首选项文件名的活动类名称。
以下是获取偏好的代码
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);
存储值的代码也与共享首选项的代码相同。
SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
您还可以使用其他方法,例如在数据库中存储活动状态。注意Android还包含一个名为android.preference的包。该包定义了用于实现应用程序首选项UI的类。
答案 2 :(得分:0)
请勿使用偏好设置保存Facebook数据:
请在Oncreate方法中使用此代码:
if (isLoggedIn()) {
layout_after_fb_login.setVisibility(View.VISIBLE);
updateUI();
} else {
layout_after_fb_login.setVisibility(View.GONE);
}
////
private boolean isLoggedIn() {
AccessToken accesstoken = AccessToken.getCurrentAccessToken();
return !(accesstoken == null || accesstoken.getPermissions().isEmpty());
}
private void updateUI() {
Profile profile = Profile.getCurrentProfile();
if (null != profile) {
profilePictureView.setProfileId(profile.getId());
userNameView.setText(String.format("%s %s", profile.getFirstName(), profile.getLastName()));
layout_after_fb_login.setVisibility(View.VISIBLE);
} else {
layout_after_fb_login.setVisibility(View.GONE);
}
}