2
當我登錄到我的Android應用程序與facebook登錄(啓用SSO)掛鉤時,它自動登錄到我的應用程序作爲我輸入登錄憑據。這裏是我的代碼plz幫助我出去我硝基甲苯KNW。爲什麼這樣做的原因在應用Facebook登錄和重定向沒有正常發生
package ten.mob.socialapp;
import java.io.IOException;
import java.net.MalformedURLException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
public class FoodStarLoginActivity extends Activity{
private static final String faceBookAppId = "430255993683523";
static Facebook facebook = new Facebook(faceBookAppId);
private SharedPreferences mPrefs;
AsyncFacebookRunner asyncRunner;
Context context;
String logout = null;
@Override
public void onCreate(Bundle savedInstanceState){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = getPreferences(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);
}
if(expires != 0){
facebook.setAccessExpires(expires);
}
if(!facebook.isSessionValid()){
logInAuthorization();
}
Intent getintent = getIntent();
logout=getintent.getStringExtra("logout");
if(logout == null && facebook.isSessionValid()){
startSearchMyActivity();
}else{
facebookLogout();
if(!facebook.isSessionValid()){
logInAuthorization();
if(facebook.isSessionValid())
startSearchMyActivity();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void facebookLogout() {
try{
facebook.logout(getApplicationContext());
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
private void startSearchMyActivity() {
Intent intent = new Intent(this,SearchMyActivity.class);
startActivity(intent);
}
private void logInAuthorization(){
facebook.authorize(this, new DialogListener(){
@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();
}
@Override
public void onFacebookError(FacebookError error){}
@Override
public void onError(DialogError e){}
@Override
public void onCancel(){}
});
}
}
沒有用也不例外發現 – Chethan