简体   繁体   中英

android facebook already logged in

I want to check if the user is already logged in with Facebook or not, on splash screen. I try this but its not working...

this is my main activity ... how i check that user already logged in

public class MainActivity extends Activity {

TextView txt1, txt2, txt3;
String Name, Id, Birth;
AccessTokenTracker accessTokenTracker;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static String url_create_product = "http://192.168.1.3:8000/butter_php/fb_user.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

public CallbackManager callback;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    TextView txt = (TextView) findViewById(R.id.txt_skip);
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
            updateWithToken(newAccessToken);
        }
    };

    updateWithToken(AccessToken.getCurrentAccessToken());

    txt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent skip = new Intent(MainActivity.this,
                    Awaiting_Approval.class);
            startActivity(skip);

        }
    });
    LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
    callback = CallbackManager.Factory.create();



    authButton.registerCallback(callback,
            new FacebookCallback<LoginResult>() {



                @Override
                public void onSuccess(LoginResult result) {


                    // TODO Auto-generated method stub



                    GraphRequest request = GraphRequest.newMeRequest(
                            AccessToken.getCurrentAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {


                                @Override
                                public void onCompleted(JSONObject object,
                                        GraphResponse response) {
                                    String name = object.optString("name");
                                    String id = object.optString("id");
                                    String link = object.optString("link");
                                    String birth = object
                                            .optString("birthday");
                                    // String gender
                                    // =object.optString("gender");
                                    // String location =
                                    // object.optJSONObject("location").optString("name");

                                    Log.d("Name", name);
                                    Log.d("Id", id);
                                    Log.d("Link", link);
                                    Log.d("Birthday", birth);
                                    // Log.d("Gender",gender);
                                    // Log.d("Location", location);

                                    /*
                                     * ProfilePictureView pictureView
                                     * =(ProfilePictureView
                                     * )findViewById(R.id.profilePicture);
                                     * 
                                     * pictureView.setCropped(true);
                                     * pictureView.setProfileId(id);
                                     */

                                    txt1 = (TextView) findViewById(R.id.textView_1);
                                    txt2 = (TextView) findViewById(R.id.textView_2);
                                    txt3 = (TextView) findViewById(R.id.textView_3);
                                    // txt4=(TextView)findViewById(R.id.textView4);

                                    txt1.setText(name);
                                    txt2.setText(id);
                                    txt3.setText(birth);
                                    // txt4.setText(gender);

                                    /*
                                     * Picasso.with(context).load(
                                     * "https://graph.facebook.com/" + id+
                                     * "/picture?type=large").into(img1);
                                     */

                                    /*
                                     * Intent intent = new
                                     * Intent(MainActivity.this,
                                     * MainActivity.class);
                                     * 
                                     * 
                                     * 
                                     * 
                                     * 
                                     * 
                                     * Bundle bundle =new Bundle();
                                     * bundle.putString
                                     * ("Name",txt1.getText().toString());
                                     * bundle
                                     * .putString("Id",txt2.getText().toString
                                     * ());
                                     * bundle.putString("Birthday",txt3.
                                     * getText().toString());
                                     * //bundle.putString
                                     * ("Gender",txt4.getText().toString());
                                     * 
                                     * intent.putExtras(bundle);
                                     * startActivity(intent);
                                     * 
                                     * finish();
                                     */

                                    new DoRegister().execute();

                                    Name = txt1.getText().toString();
                                    Id = txt2.getText().toString();
                                    Birth = txt3.getText().toString();

                                }
                            });

                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,link,birthday");
                    request.setParameters(parameters);
                    request.executeAsync();

                }

                @Override
                public void onError(FacebookException error) {
                    // TODO Auto-generated method stub
                    System.out.println("onError");

                }

                @Override
                public void onCancel() {
                    // TODO Auto-generated method stub
                    System.out.println("onCancel");
                }


            });

    authButton
            .setReadPermissions(Arrays
                    .asList("public_profile,email,user_friends,user_location, user_birthday"));



}



private void updateWithToken(AccessToken currentAccessToken) {

    if (currentAccessToken != null) {

                Intent i = new Intent(MainActivity.this, Home_Page.class);
                startActivity(i);

                finish();
            }

    else {



                Intent i = new Intent(MainActivity.this, MainActivity.class);
                startActivity(i);

                finish();
            }
}





class DoRegister extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", Name));
        params.add(new BasicNameValuePair("id", Id));
        params.add(new BasicNameValuePair("birth", Birth));

        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product

                Singleton.GetInstance().setUserId(Id);
                Singleton.GetInstance().setUserName(Name);
                Singleton.GetInstance().setUserBith(Birth);
                Intent it = new Intent(MainActivity.this,
                        User_Details.class);
                Bundle bundle = new Bundle();

                bundle.putString("Name", txt1.getText().toString());
                bundle.putString("Id", txt2.getText().toString());
                bundle.putString("Birthday", txt3.getText().toString());
                it.putExtras(bundle);

                startActivity(it);
                // closing this screen


                finish();
            } else {
                // failed to create product
                Log.d(" Response", json.getString("message"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callback.onActivityResult(requestCode, resultCode, data);
    /*
     * if (resultCode == RESULT_OK) { Intent secondActivityIntent = new
     * Intent(this, Redirect_page.class);
     * startActivity(secondActivityIntent); }
     */

}

}

正如我在这里看到的那样,在MainActivityonCreate中,您检查了调用updateWithToken ,该调用检查当前的AccessToken,如果不是Null,则将打开MainActivity AGAIN,它将检查AccessToken,如果不为null,则会一次又一次打开它。 .....您需要检查AccessToken是否为null,否则什么也不做

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