2015-06-14 115 views
3

花了幾天的時間試圖找出這一點,我無法想出任何有效的工作。使用我的代碼,它顯示我接受並登錄的權限表單,但它沒有進入onSuccess,onError或onCancel。任何時候我點擊按鈕,它都不會做任何事情。 LogCat上沒有錯誤。我不知道我哪裏錯了。Android FacebookSDK V4.20使用登錄管理器登錄和註銷

fb = (Button) findViewById(R.id.fb_button); 
    fb.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds); 
      callbackManager = CallbackManager.Factory.create(); 

      LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() { 
       @Override 
       public void onSuccess(LoginResult loginResult) { 
        Log.d("kkkkkk","kkllkl"); 
       } 

       @Override 
       public void onCancel() { 
        Log.d("kkkkkk","kkllkl2"); 
       } 

       @Override 
       public void onError(FacebookException error) { 
        Log.d("kkkkkk","kkllkl4"); 
       } 
      }); 


     } 
    }); 

FacebookSdk.sdkInitialize(this.getApplicationContext());super.onCreate(savedInstanceState);

+0

了Facebook SDK V4我們可以註銷使用這個鏈接http://stackoverflow.com/questions/18529874/android-clear-facebook-access-token/32324398#32324398 –

回答

3

我終於想通了之後被初始化。我改變了代碼一點從最初的代碼如下:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 
    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Log.d("kkkkkk","kkllkl"); 
     } 

     @Override 
     public void onCancel() { 
      Log.d("kkkkkk","kkllkl2"); 
     } 

     @Override 
     public void onError(FacebookException error) { 
      Log.d("kkkkkk","kkllkl4"); 
     } 
    }); 

,然後在點擊監聽按鈕的,我不得不撥打電話進行登錄。

fb.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds); 

     } 
    }); 

也不要忘記添加onActivityResult。我也犯了這個錯誤。

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

完整的代碼如下。

public class MainActivity extends ActionBarActivity { 

    CallbackManager callbackManager; 
    Button but, but2; 
    AccessTokenTracker accessTokenTracker; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     but = (Button) findViewById(R.id.button); 

     FacebookSdk.sdkInitialize(getApplicationContext()); 
     callbackManager = CallbackManager.Factory.create(); 




      LoginManager.getInstance().registerCallback(callbackManager, 
        new FacebookCallback<LoginResult>() { 
         @Override 
         public void onSuccess(LoginResult loginResult) { 
          // App code 
         } 

         @Override 
         public void onCancel() { 
          // App code 
          // savedInstanceState 
         } 

         @Override 
         public void onError(FacebookException exception) { 
          // App code 
         } 
        }); 
      LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); 

    // } 
      accessTokenTracker = new AccessTokenTracker() { 
       @Override 
       protected void onCurrentAccessTokenChanged(
         AccessToken oldAccessToken, 
         AccessToken currentAccessToken) { 

       } 
      }; 

      but.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        getUserData(AccessToken.getCurrentAccessToken()); 
        Log.d("Access Token: ", AccessToken.getCurrentAccessToken().toString()); 
       } 
      }); 
     } 


    public void getUserData(AccessToken accessToken){ 
     GraphRequest request = GraphRequest.newMeRequest(
       accessToken, 
       new GraphRequest.GraphJSONObjectCallback() { 
        @Override 
        public void onCompleted(
          JSONObject object, 
          GraphResponse response) { 
         //JSONParser parser=new JSONParser(); 
         // Data data = new Gson().fromJson(json, Data.class); 
         long ID = 0; 

       try { 
        String name = response.getRawResponse(); 
        Toast.makeText(MainActivity.this, "response is: "+object.toString(), Toast.LENGTH_LONG).show(); 
       } 
       catch (Exception e){ 
        Toast.makeText(MainActivity.this, "error is: "+e.toString(), Toast.LENGTH_LONG).show(); 
       } 

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

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

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     accessTokenTracker.stopTracking(); 
    } 
} 
+0

耶。這也是一個不錯的方法。 – uchman21