如果用户登录后如何在Android应用程序中自动登录Facebook?

时间:2014-12-04 07:17:15

标签: android facebook facebook-graph-api

我在我的android应用程序中使用graph api连接到facebook.Now我正在做的是为我的Facebook类创建一个实例并每次从主活动调用login方法。如果用户登录一次使用应用程序,以及下次用户打开应用程序时,应该使用之前的活动facebook会话直接登录。如何做到这一点?Sombody请帮忙......

登录按钮单击方法

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;

 }

 });

}

1 个答案:

答案 0 :(得分:0)

您可以查看Session.getActiveSession();

例如onCompleted()方法

中使用此功能
Session facebooksession = Session.getActiveSession();
if(facebooksession != null) {
//Second time login
}
else{
//First time login
}