我已经创建了一个应用程序,即时通讯登录Facebook,并从Facebook获取数据。,但我的注销方法不起作用,有什么问题,我无法理解,在设备中启动应用程序无论我是谁我填写开始它总是从那个onle获取数据,它没有登出,我无法登录另一个id.please建议我。
MainActivity
public class MainActivity extends Activity {
Facebook fb;
Button login,getData,logout;
ImageView ig;
String app_id;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
app_id= getString(R.string.app_id);
fb= new Facebook(app_id);
login=(Button) findViewById(R.id.login);
logout=(Button) findViewById(R.id.logout);
getData=(Button) findViewById(R.id.getData);
// ig= (ImageView) findViewById(R.id.profile_pic);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginToFacebook();
}
});
getData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getProfileInformation();
}
});
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fb.isSessionValid()){
logoutFromFacebook();
}
}
});
mAsyncRunner = new AsyncFacebookRunner(fb);
//updateButtonImage();
}
@SuppressWarnings("deprecation")
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
fb.setAccessToken(access_token);
Toast.makeText(getApplicationContext(), "You Already Login",
Toast.LENGTH_SHORT).show();
}
if (expires != 0) {
fb.setAccessExpires(expires);
Toast.makeText(getApplicationContext(), "You Already Login",
Toast.LENGTH_SHORT).show();
access_token=null;
expires=0;
}
if (!fb.isSessionValid()) {
fb.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",
fb.getAccessToken());
editor.putLong("access_expires",
fb.getAccessExpires());
editor.commit();
// Making Login button invisible
login.setVisibility(View.VISIBLE);
// Making logout Button visible
getData.setVisibility(View.VISIBLE);
}
@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);
fb.authorizeCallback(requestCode, resultCode, data);
}
@SuppressWarnings("deprecation")
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
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");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onIOException(IOException e, Object state) {
}
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
public void onFacebookError(FacebookError e, Object state) {
}
});
}
@Deprecated
public void logoutFromFacebook() {
mAsyncRunner.logout(MainActivity.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() {
// make Login button visible
login.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
getData.setVisibility(View.INVISIBLE);
}
});
}
}
@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) {
}
});
}
}
logcat的
04-16 17:42:34.226: E/AndroidRuntime(32465): FATAL EXCEPTION: Thread-4017
04-16 17:42:34.226: E/AndroidRuntime(32465): java.lang.IllegalArgumentException: Invalid context argument
04-16 17:42:34.226: E/AndroidRuntime(32465): at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
04-16 17:42:34.226: E/AndroidRuntime(32465): at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:286)
04-16 17:42:34.226: E/AndroidRuntime(32465): at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:310)
04-16 17:42:34.226: E/AndroidRuntime(32465): at com.facebook.Session.closeAndClearTokenInformation(Session.java:614)
04-16 17:42:34.226: E/AndroidRuntime(32465): at com.facebook.android.Facebook.logoutImpl(Facebook.java:665)
04-16 17:42:34.226: E/AndroidRuntime(32465): at com.facebook.android.AsyncFacebookRunner$1.run(AsyncFacebookRunner.java:89)
答案 0 :(得分:1)
试试这个:
我希望这会对你有所帮助......
if( mFacebook.isSessionValid() ) {
try {
String str=mFacebook.logout(getApplicationContext());
SessionStore.clear(getApplicationContext());
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
通过在OnCreate
之后将此代码放在MainActivity中,从项目中获取Key哈希值 try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures)
{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
Log.e("name not found", e.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("no such an algorithm", e.toString());
}
转到logcat并从那里复制Key哈希然后转到https://developers.facebook.com/并单击您创建的应用程序,然后转到设置,将您的密钥粘贴到Key Hashes框中,然后单击Save changes。确保您的应用是公开的,如果不是公开的,则可供所有用户使用,然后转到设置将您的电子邮件置于“联系人电子邮箱”框中,然后点击保存更改,然后选中应用名称旁边的小圆圈。 并且对于注销只需调用logoutFromFacebook();在onClicklistener中。在设置中清除您的应用数据,然后执行注销。
答案 2 :(得分:0)
@SuppressWarnings("deprecation")
public void logoutFromFacebook() {
MainActivity.mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(final String response, Object state) {
Thread timer = new Thread() { //new thread
public void run() {
Boolean b = true;
try {
editor.remove("access_token");
editor.commit();
sleep(500);
Log.d("Logout from Facebook", response);
runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(getApplicationContext(), "You are logged out from Facebook", Toast.LENGTH_LONG).show();
login.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
getData.setVisibility(View.INVISIBLE);
}
});
while (b == true);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
};
};
timer.start();
}
@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) {
}
});
}