![](/img/trans.png)
[英]How to show facebook login on sharing if user is not logged in Android?
[英]How to provide automatic login to Facebook in android application if user once logged in ?
我在我的Android應用程序中使用graph api連接到facebook。現在我正在做的是為我的Facebook類創建一個實例,並每次從主活動中調用login方法。如果用戶使用該應用程序登錄了一次,並且下次用戶打開應用程序時,應使用上一個活動的Facebook會話直接登錄。如何執行此操作?請提供幫助...
登錄按鈕點擊方法
public void onBtnClicked(View v){
if(v.getId() == R.id.btnLogin)
{
FacebookClass na = new FacebookClass(this);
na.login();
}
}
這是我的登錄方式
public void login()
{
// start Facebook Login
Session.openActiveSession(mContext, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
sessionState = true;
Toast.makeText(mContext, "Access token :" + session.getAccessToken() + "\n" +"Expires at "+session.getExpirationDate().toLocaleString(), Toast.LENGTH_LONG).show();
Log.i("sessionToken", session.getAccessToken());
Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
access_token =session.getAccessToken();
pref = mContext.getPreferences(0);
edt = pref.edit();
Editor prefsEditor = pref.edit();
edt.putString("Access token", access_token);
edt.commit();
/**
* getting user's name and location
*/
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null)
{
dispname = user.getName();
id = user.getId();
Object userLocation = user.getLocation();
Log.i("Profile information", ""+response);
String strLocation = (userLocation != null)? ((GraphObject) userLocation).getProperty("name").toString() : "No location found";
//location.setText("Lives in " + strLocation + "!");
disploc = strLocation;
edt.putString("Username", dispname);
edt.putString("Location", disploc);
edt.commit();
((MainActivity)mContext).dispatchInformations(dispname,disploc);
}
}
}).executeAsync();
/**
* Getting user's profile picture
*/
Bundle params = new Bundle();
params.putBoolean("redirect", false);
params.putString("type", "normal");
params.putString("height", "200");
params.putString("width", "200");
new Request(
Session.getActiveSession(),
"/me/picture",
params,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
Log.i("Profile pic", ""+response);
GraphObject graph=response.getGraphObject();
JSONObject jsonObj=graph.getInnerJSONObject();
JSONObject object;
String imageURL;
try {
object = jsonObj.getJSONObject("data");
imageURL=object.getString("url");
new DownloadImageTask().execute(imageURL);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
).executeAsync();
}
}
private boolean isSubsetOf(Collection<String> subset,
Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
});
}
您可以使用Session.getActiveSession();
進行檢查Session.getActiveSession();
例如在onCompleted()
方法中使用此方法
Session facebooksession = Session.getActiveSession();
if(facebooksession != null) {
//Second time login
}
else{
//First time login
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.