2015-09-26 113 views
0

我想檢查用戶是否已經登錄Facebook或不在啓動畫面。我嘗試這一點,但它不工作...android facebook已經登錄

這是我的主要活動...我如何確認用戶已登錄

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); } 
    */ 

} 

} 

回答

0

正如我在這裏看到,在onCreate的在MainActivity,你檢查電話updateWithToken哪些檢查當前的AccessToken,如果它不是空的將打開MainActivity AGAIN,這將檢查AccessToken,如果不是null將一次又一次地打開它....... 您需要檢查AccessToken是否爲空,否則不做任何事情