我正在创建一个应用程序,用户可以使用不同的方式注册,其中一个是Facebook。 Facebook登录功能正常,但当我想注销时什么也没做。我希望它在另一个Activity中的注销按钮。 1.当我启动应用程序时,显示一个活动,其中有一个要注册的按钮。 2.如果用户已正确登录,则会显示“活动”菜单。 3.在菜单中有一个用于打开用户活动信息的按钮,这是我要放置注销按钮的位置。 4.一旦用户注销,您希望将我发送回初始Actity(登录按钮所在的位置)。 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).
}
}
答案 0 :(得分:5)
在新的Facebook SDK 4.0中,您应该致电:
LoginManager.getInstance().logOut();
在您询问Facebook SDK中的任何方法之前,请不要忘记致电FacebookSdk.sdkInitialize(getApplicationContext());
。
答案 1 :(得分:0)
我在应用程序中初始化facebook
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
并在另一个班级电话
LoginManager.getInstance().logOut();