我正在嘗試Facebook的Android集成,主要是我面臨3個問題。Android:如何檢查用戶是否在Facebook登錄或不在Facebook
1)當用戶點擊登錄Facebook按鈕,如果用戶沒有登錄,那麼它正在打開Facebook應用程序,我已經從Play商店而不是彈出窗口安裝。
2)我想查天氣用戶登錄或不如果是,那麼應用程序只要求允許該測試應用程序將訪問....,如果我按好應該移動到下一個屏幕我的意思是它不應該要求用戶憑據是電子郵件或facebbok ID和密碼。
3)註銷功能。
所以基本上我的代碼從這個link但它使用共享偏好存儲用戶數據,所以基本上如果我從我的Facebook應用程序註銷,也不會再要求登錄,所以我的問題是,如何去除這個共享偏好,而不是共享偏好,我應該使用什麼。
而最後一件事是我已經實現了註銷功能,但它不能正常工作我不知道爲什麼。
下面是我的完整代碼。
public class FacebookLogin extends Activity {
Button facebookLogin;
Button facebookLogout;
private static String APP_ID = "myapp-key";
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
private SharedPreferences mPrefs;
String access_token;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.facebbok_login);
facebookLogin = (Button) findViewById(R.id.facebookLogin);
facebookLogout = (Button) findViewById(R.id.facebooklogout);
mAsyncRunner = new AsyncFacebookRunner(facebook);
facebookLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
loginToFacebook();
}
});
facebookLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
logoutFromFacebook();
}
});
}
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
Intent mIntent = new Intent(FacebookLogin.this, ShowVideo.class);
startActivity(mIntent);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
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) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
Intent mIntent = new Intent(FacebookLogin.this,
ShowVideo.class);
startActivity(mIntent);
}
@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);
}
public void logoutFromFacebook() {
Log.d("Image Button", "Inside logout");
mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent mIntent = new Intent(FacebookLogin.this,
SplashScreen.class);
startActivity(mIntent);
}
});
}
}
@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) {
}
});
}
}
您正在使用舊Facebook的api,這不是一個好習慣。我真的建議你去facebook sdk 3.6。大多數方法已被棄用。 [按照這個](https://developers.facebook.com/docs/android/getting-started/)它是自我解釋。有一個叫UiLifeCylceHelper的類可以輕鬆解決你的前兩個問題。祝你好運 – Coderji
@Coderji,好吧,我已經使用faebook sdk 3.6,現在你能告訴我如何實現我的loginToFacebook()函數。 – InnocentKiller