[英]How to start a new activity and hide login page after successful login in android facebook app and get logout button on new page
[英]How to hide the Facebook Login page activity after successful login in android?
我是Android的新手。 我在Android
應用程序中加入了Facebook
。 我有一個帶有按鈕的登錄頁面。 單擊該按鈕后,將顯示FB登錄頁面。Facebook成功登錄后,用戶可以轉到MainActivity。 成功登錄FB后,我想隱藏我的登錄頁面。 也就是說,當我重新加載應用程序時,如果用戶已經登錄,則應該僅顯示MainActivity而不是“登錄”頁面。
我的代碼如下:
public class Login extends Activity {
private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";
//private TextView textInstructionsOrLink;
private ImageButton buttonLoginLogout;
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
private Button b;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
Databasehandler db=new Databasehandler(this);
HashMap<String, String>map=new HashMap<String, String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
buttonLoginLogout = (ImageButton)findViewById(R.id.imageButton1);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
updateView();
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
final String t1=user.getId();
final String t2=user.getName();
// SharedPreferences myPrefs =getSharedPreferences("myprefs",MODE_WORLD_READABLE);
int count=db.getme();
if(count==0)
{
map.put("uid",t1.toString());
map.put("name",t2.toString());
db.insertnewuser(map);
}
}
});
Intent ik=new Intent(Login.this,MainActivity.class);
ik.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ik);
} else {
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
}
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
}
只需在updateView
方法內添加以下代碼updateView
。
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
因此,您的完整方法將類似於以下代碼。
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
final String t1=user.getId();
final String t2=user.getName();
// SharedPreferences myPrefs =getSharedPreferences("myprefs",MODE_WORLD_READABLE);
int count=db.getme();
if(count==0)
{
map.put("uid",t1.toString());
map.put("name",t2.toString());
db.insertnewuser(map);
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}
}
因此,基本上,只要打開Facebook會話,它將自動移至下一個屏幕。
//成功登錄后,隱藏facebook按鈕
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
//after successful login into the facebook it hide the logout button
loginButton.setVisibility(View.GONE);
}
//if you want to logout after login to facebook successfully then you want to write this line inside the onSccess() method at the end
//after successful login into the facebook it hide the logout button
LoginManager.getInstance().logOut();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.