2015-04-07 269 views
0

嗨在這裏,我創建登錄與Facebook沒有登錄按鈕。它正常工作,但在關閉的圖表webkit登錄表單,它再次打開連續請求許可彈出。給我解決方案。用Facebook無登錄按鈕登錄Android

private void FacebookLogin() { 
    // TODO Auto-generated method stub 

    final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
      this, Arrays.asList("email")); 
    Session openActiveSession = Session.openActiveSession(this, true, 
      new Session.StatusCallback() { 
       @Override 
       public void call(Session session, SessionState state, 
         Exception exception) { 

        if (session.isOpened()) { 
         session.requestNewReadPermissions(newPermissionsRequest); 
         Request getMe = Request.newMeRequest(session, 
           new GraphUserCallback() { 
            @Override 
            public void onCompleted(GraphUser user, 
              Response response) { 
             if (user != null) { 
              org.json.JSONObject graphResponse = response 
                .getGraphObject() 
                .getInnerJSONObject(); 
              String email = graphResponse 
                .optString("email"); 
              String id = graphResponse 
                .optString("id"); 
             } 
            } 
           }); 
         getMe.executeAsync(); 
        } else { 
         if (!session.isOpened()) 
          Log.d("FACEBOOK", "!session.isOpened()"); 
         else 
          Log.d("FACEBOOK", "isFetching"); 

        } 
       } 
      }); 


} 

@Override 
protected void onActivityResult(int arg0, int arg1, Intent arg2) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(arg0, arg1, arg2); 
    Session.getActiveSession().onActivityResult(this, arg0, arg1, arg2); 
} 
+0

你是說,只要對話框關閉,它會再次打開? –

+0

發佈整個片段代碼 –

+0

檢查此https://developers.facebook.com/docs/facebook-login/android/v2.3 –

回答

0

試試這個

public class LoginFragment extends Fragment{ 
     boolean isFetching = false; 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
       View view = inflater.inflate(R.layout.login_fragment, null); 

       Button button = (Button) view.findViewById(R.id.login_button); 
       button.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         performFacebookLogin(); 
        } 
       }); 
       return view; 
      } 
      private void performFacebookLogin() 
      { 
       Log.d("FACEBOOK", "performFacebookLogin"); 
       final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(getActivity(), Arrays.asList("email")); 
       Session openActiveSession = Session.openActiveSession(getActivity(), true, new Session.StatusCallback() 
       { 
        @Override 
        public void call(Session session, SessionState state, Exception exception) 
        { 
         Log.d("FACEBOOK", "call"); 
         if (session.isOpened() && !isFetching) 
         { 
          Log.d("FACEBOOK", "if (session.isOpened() && !isFetching)"); 
          isFetching = true; 
          session.requestNewReadPermissions(newPermissionsRequest); 
          Request getMe = Request.newMeRequest(session, new GraphUserCallback() 
          { 
           @Override 
           public void onCompleted(GraphUser user, Response response) 
           { 
            Log.d("FACEBOOK", "onCompleted"); 
            if (user != null) 
            { 
             Log.d("FACEBOOK", "user != null"); 
             org.json.JSONObject graphResponse = response.getGraphObject().getInnerJSONObject(); 
             String email = graphResponse.optString("email"); 
             String id = graphResponse.optString("id"); 
             String facebookName = user.getUsername(); 
             if (email == null || email.length() < 0) 
             { 
              Toast.makeText(getActivity(), 
                "An email address is required for your account, we could not find an email associated with this Facebook account. Please associate a email with this account or login the oldskool way.", Toast.LENGTH_LONG).show(); 
              return; 
             } 
            } 
           } 
          }); 
          getMe.executeAsync(); 
         } 
         else 
         { 
          if (!session.isOpened()) 
           Log.d("FACEBOOK", "!session.isOpened()"); 
          else 
           Log.d("FACEBOOK", "isFetching"); 

         } 
        } 

       }); 

} 
} 

現在,在您的片段添加一個按鈕佈局

<Button 
    android:id="@+id/login_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Login With Facebook" /> 

它的工作對我來說,只有一次彈出請求允許嘗試,讓我知道

+0

我想使用沒有Facebook登錄按鍵 – DroidVasu

0

我發現了兩種方法。

第一個 - 你添加到XML facebook按鈕並隱藏(設置visibility = GONE)它。初始化此按鈕並在您自定義事件調用facebookButton.performClick();

第二種方法 - 使用這部分代碼:當你想打電話彈出

callbackManager = CallbackManager.Factory.create(); 
    List<String> permission = new ArrayList<String>(); 
    permission.add("email"); 
    LoginManager loginManager = LoginManager.getInstance(); 

    loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      if (facebookDialog.isShowing()) { 
       loadingProgressBar.setVisibility(View.VISIBLE); 
       singViaFacebook.setText(R.string.dialog_facebook_loggin); 
      } 
      loadProfile(); 
     } 

     @Override 
     public void onCancel() { 
      lockLoginButton(true); 
      startTimer(); 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      lockLoginButton(true); 
      startTimer(); 
     } 
    }); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 
} 

,要求把這段代碼:

loginManager.logInWithReadPermissions(this, permission) 

作品與Facebook SDK [4,5)

0

First Declare Callback manager like,

private CallbackManager callbackManager;

private AccessToken accessToken;

把這個代碼中的OnCreate()::

callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance()。registerCallback(callbackManager,新FacebookCallback(){

 @Override 
     public void onSuccess(LoginResult loginResult) { 
      accessToken = loginResult.getAccessToken(); 
      getFacebookUserProfile(accessToken); 
     } 

     @Override 
     public void onCancel() { 
      Toast.makeText(StartUpActivity.this, "Login with facebook canceled.", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onError(FacebookException error) { 
      Toast.makeText(StartUpActivity.this, error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 

然後點擊分配給您的按鈕一樣,

btnFacebook.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       accessToken = AccessToken.getCurrentAccessToken(); 
       if (accessToken != null) { 
        getFacebookUserProfile(accessToken); 
       } else { 
        LoginManager.getInstance().logInWithReadPermissions(StartUpActivity.this, Arrays.asList("public_profile", "email")); 
       } 
      } 
     }); 

然後覆蓋,

@Override                  
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) { 
      callbackManager.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

然後使用此功能用於獲取用戶配置文件,

private void getFacebookUserProfile(AccessToken accessToken) { 
     GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 

      @Override 
      public void onCompleted(JSONObject object, GraphResponse response) { 
       try { 
        authenticateUser(object); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     Bundle parameters = new Bundle(); 
     parameters.putString("fields", "id, name, first_name,last_name, email"); 
     request.setParameters(parameters); 
     request.executeAsync(); 
    } 

希望它適合你