成功登入Android后如何隐藏Facebook登入页面活动? [英] How to hide the Facebook Login page activity after successful login in android?
问题描述
Android
应用程序中,我已经有了$ code> Facebook 。我有一个带有按钮的登录页面。点击该按钮,就会出现FB登录页面。成功登录Facebook后,用户将进入MainActivity。成功登录FB后,我想隐藏我的登录页面。也就是说,当我重新加载应用程序时,如果用户已经登录,那么MainActivity只应该被分配,而不是登录页面。 我的代码如下:
public class Login extends Activity {
private static final String URL_PREFIX_FRIENDS =https://graph.facebook.com /我的/朋友=的access_token?;
//私有TextView textInstructionsOrLink;
private ImageButton buttonLoginLogout;
public static final String PREFS_PRIVATE =PREFS_PRIVATE;
私人按钮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.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.getActiveSession();
Session.saveSession(session,outState);
}
@Override
protected void onDestroy(){
super.onDestroy();
}
private void updateView(){
会话session = Session.getActiveSession();
if(session.isOpened()){
Request.executeMeRequestAsync(session,new Request.GraphUserCallback(){
@Override
public void onCompleted(GraphUser user ,响应响应){
// TODO自动生成的方法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.getActiveSession();
if(!session.isOpened()&!session.isClosed()){
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this,true,statusCallback);
}
}
私有类SessionStatusCallback实现Session.StatusCallback {
@Override
public void call(Session session,SessionState state,Exception exception){
更新视图();
}
}
}
意图intent = new Intent(Login.this,MainActivity.class);
startActivity(intent);
所以你的完整方法看起来像下面的代码。
private void updateView(){
pre>
Session session = Session.getActiveSession();
if(session.isOpened()){
Request.executeMeRequestAsync(session,new Request.GraphUserCallback(){
@Override
public void onCompleted(GraphUser user ,响应响应){
// TODO自动生成的方法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 = new Intent(Login.this,MainActivity.class);
startActivity(intent);
}
}
所以基本上会自动移动到下一个scr每当Facebook会议开放时,都可以使用。
I am a newbiew in Android. I have intergarted
Android
app. I have a Login page with a button. On click of that button the FB login page appears.After the successful login of Facebook, the user gets to go to the MainActivity. I want to hide my Login page after the successful login of FB. That is, when I reload the app, if the user has already logged in then the MainActivity only should be dispalyed and not the Login page.My code is as follows:
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(); } } }
解决方案Just add below code inside
updateView
method.Intent intent = new Intent(Login.this, MainActivity.class); startActivity(intent);
So your full method will look something like below code.
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); } }
So basically it will automatically moves to next screen whenever facebook session is opened.
这篇关于成功登入Android后如何隐藏Facebook登入页面活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!