简体   繁体   中英

How to provide automatic login to Facebook in android application if user once logged in ?

I'm using graph api in my android application to connect to facebook.Now what i'm doing is creating an instance for my Facebook class and calling the login method from main activity each time.If user login one time using the application,and the next time when the user opens application,it should directly login using the previous active facebook session..How to do this ?Sombody please help...

Login button click method

public void onBtnClicked(View v){
            if(v.getId() == R.id.btnLogin)
            {

                FacebookClass na = new FacebookClass(this);
                na.login();
             }
}

This is my login method

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;

 }

 });

}

You can check with Session.getActiveSession();

For example Use this inside onCompleted() method

Session facebooksession = Session.getActiveSession();
if(facebooksession != null) {
//Second time login
}
else{
//First time login
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM