Android:从另一个活动中注销 Facebook
Posted
技术标签:
【中文标题】Android:从另一个活动中注销 Facebook【英文标题】:Android : Logout of Facebook from another Activity 【发布时间】:2015-05-31 01:33:38 【问题描述】:我正在创建一个应用程序,用户可以在其中以不同的方式注册,其中一种是使用 Facebook。 Facebook 登录功能正常,但当我想注销时什么也不做。我希望它在另一个活动中的注销按钮。 1. 当我启动应用程序时,会显示一个 Activity,其中有一个注册按钮。 2. 如果用户正确登录,则显示活动菜单。 3.在菜单中有一个按钮,打开一个用户活动信息,这是我要放置我的注销按钮的地方。 4.一旦用户注销希望您将我送回最初的活动(登录按钮所在的位置)。 5. 我希望删除或重置首选项,因为如果我注册正确不会询问我的记录并直接进入菜单。
这是我使用的代码:
公共类 FacebookLogin 扩展 ActionBarActivity
//
SharedPreferences pref;
//
// Your Facebook APP ID
private static String APP_ID = "94032093……."; //
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "androidSSO_data";
SharedPreferences mPrefs;
// Buttons
ImageView btnFbLogin;
Button Logout;
TextView txttest;
SharedPreferences pref_2;
String nun_2, name_2, email_2;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_register);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
btnFbLogin = (ImageView) findViewById(R.id.imgLogFacebook);
Logout= (Button) findViewById(R.id.btnLogout);
pref=getSharedPreferences("bus",Context.MODE_PRIVATE);
//
//
txttest = (TextView)findViewById(R.id.txttest);
pref_2=getSharedPreferences("facebook",Context.MODE_PRIVATE);
//
mAsyncRunner = new AsyncFacebookRunner(facebook);
/**
* Login button
* */
btnFbLogin.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Log.d("Image Button", "button Clicked");
loginToFacebook();
);
//I WANT MY LOGOUT BUTTON LIKE THIS
Logout.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
logoutToFacebook();
);
if(pref.contains("num")==true ||pref_2.contains("num_2")==true)
Intent mainIntent = new Intent().setClass(FacebookLogin.this, Menu.class);
startActivity(mainIntent);
finish();
else
if(isNetworkAvailable())
Toast.makeText(getApplicationContext(), "Welcome... Login please",Toast.LENGTH_LONG).show();
/**
* Login facebook
* */
@SuppressWarnings("deprecation")
public void loginToFacebook()
Toast.makeText(getApplicationContext(), "wait a moment please...", Toast.LENGTH_LONG).show();
mPrefs = getSharedPreferences("face",Context.MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null)
facebook.setAccessToken(access_token);
//i f is loged star
Intent menu = new Intent(FacebookLogin.this, Menu.class);
startActivity(menu);
finish();
Log.d("FB Sessions", "" + facebook.isSessionValid());
if (expires != 0)
facebook.setAccessExpires(expires);
Toast.makeText(getApplicationContext(), "Error Toast.LENGTH_LONG).show();
if (!facebook.isSessionValid())
facebook.authorize(this,
new String[] "email", "publish_stream" ,
new DialogListener()
@Override
public void onCancel()
// Function to handle cancel event
@Override
public void onComplete(Bundle values)
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
//save preferences to login
txttest.setText("Bus");
txttest.setVisibility(View.INVISIBLE);
getProfileInformation();
if(pref_2.contains("num_2")==false)
if(txttest.getText().toString().matches("Bus"))
num_2=txttest.getText().toString();
Editor editor2 = pref_2.edit();
editor2.putString("num_2", num_2);
editor2.commit();
num_2=pref_2.getString("num_2", null);
else
Toast.makeText(getApplicationContext(), "You are login", Toast.LENGTH_LONG).show();
//end preferences
Intent menu = new Intent(FacebookLogin.this, Menu.class);
startActivity(menu);
finish();
@Override
public void onError(DialogError error)
// Function to handle error
@Override
public void onFacebookError(FacebookError fberror)
// Function to handle Facebook errors
);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
/**
* user info
* */
@SuppressWarnings("deprecation")
public void getProfileInformation()
mAsyncRunner.request("me", new RequestListener()
@Override
public void onComplete(String response, Object state)
Log.d("Profile", response);
String json = response;
try
// Facebook Profile JSON data
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
name_2 = name.toString();
Editor editor = pref_2.edit();
editor.putString("name_2", name_2);
editor.commit();
name_2 = pref_2.getString("name_2", null);
// getting email of the user
final String email = profile.getString("email");
email_2 = email.toString();
Editor editor2 = pref_2.edit();
editor2.putString("email_2", email_2);
editor2.commit();
email_2 = pref_2.getString("email_2", null);
runOnUiThread(new Runnable()
@Override
public void run()
//Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
);
catch (JSONException e)
e.printStackTrace();
@Override
public void onIOException(IOException e, Object state)
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state)
@Override
public void onMalformedURLException(MalformedURLException e,
Object state)
@Override
public void onFacebookError(FacebookError e, Object state)
);
Public static void logoutToFacebook ()
//HERE I WANT TO WRITE MY LOGOUT CODE AND CALL FROM ANOTHER ACTIVITY (PROFILE ACTIVITY).
【问题讨论】:
如果有人建议我这个代码: public static void callFacebookLogout(Context context) Session session = Session.getActiveSession(); if (session != null) if (!session.isClosed()) session.closeAndClearTokenInformation(); //如果保存则清除您的偏好 else session = new Session(context); Session.setActiveSession(session); session.closeAndClearTokenInformation(); //如果保存则清除您的偏好 请告诉我我调用了我的方法 logoutToFacebook () ; 【参考方案1】:在新的 Facebook SDK 4.0 中,您应该调用:
LoginManager.getInstance().logOut();
在询问 Facebook SDK 中的任何方法之前,不要忘记致电 FacebookSdk.sdkInitialize(getApplicationContext());
。
【讨论】:
【参考方案2】:我在应用程序中初始化 facebook
public class MyApplication extends Application
@Override
public void onCreate()
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
并在另一个班级电话中
LoginManager.getInstance().logOut();
【讨论】:
以上是关于Android:从另一个活动中注销 Facebook的主要内容,如果未能解决你的问题,请参考以下文章
如果他进入后台或在不活动几分钟后如何从 android 应用程序中注销用户
Android在C ++ JNI代码中从另一个活动类调用Java函数