我正在构建一个应用程序,用户首先通过Facebook登录然后它转到另一个地图活动。每当我重新启动我的应用程序时,它显示注销按钮不登录或只是去另一个活动加上我想保存我的客户的电子邮件。这是我的代码 -
public class MainActivity extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
sp = getPreferences(MODE_PRIVATE);
String access_token = sp.getString("access_token",null);
long expires = sp.getLong("access_expires",0);
if (access_token != null){
}
info = (TextView) findViewById(R.id.info);
loginButton = (LoginButton) findViewById(R.id.login_button);
List<String> permissionNeeds = Arrays.asList("email", "user_birthday", "public_profile");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String token = loginResult.getAccessToken().getToken();
Log.d("LOGIN_SUCCESS", "Success");
loginButton.setVisibility(View.INVISIBLE); //<- IMPORTANT
Intent intent = new Intent(MainActivity.this,MapsActivity.class);
startActivity(intent);
finish();//<- IMPORTANT
}
@Override
public void onCancel() {
info.setText("Login attempt Canceled");
}
@Override
public void onError(FacebookException error) {
info.setText("Login attempt failed");
}
});
AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
// Set the access token using `q
// currentAccessToken when it's loaded or set.
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
public void imageView (View v) {
ImageView img = (ImageView) findViewById(R.id.loofreLogo);
if (img != null) {
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.loofre.com"));
startActivity(intent);
}
});
}
}
public void InsOnclick(View args0) {
if (args0.getId()==R.id.Bins){
Intent intent = new Intent(this,instructionSlide.class);
this.startActivity(intent);
}
}
public void AboutUs (View args1){
if (args1.getId()==R.id.Babout){
Intent intent = new Intent(this,AboutUs.class);
this.startActivity(intent);
}
}
}
答案 0 :(得分:0)
要在共享偏好设置中保存数据,请执行以下操作:
SharedPreferences preferencesPut = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("nameKEY", "name");
editor.putString("emailKEY", "email");
editor.putBoolean("done", true);
editor.commit();
这将保存您的姓名和电子邮件。字符串&#34; KEY&#34;是一个特定的键,它指定您想要知道的共享首选项的值。然后从共享首选项中获取数据:
SharedPreferences preferencesGet = getSharedPreferences("KEY", Context.MODE_PRIVATE);
String name = preferencesGet.getString("nameKEY", "default");
String email = preferencesGet.getString("emailKEY", "default");
您将获得姓名和电子邮件的字符串&#39; name&#39;和&#39;电子邮件&#39;。你会得到价值&#34;默认&#34;如果您无法在共享首选项中保存数据或无法获取数据。