2016-09-02 71 views
0

我正在整合Facebook登錄在我的Android應用程序中,當我想要一個簡單的按鈕,然後點擊我想獲取用戶details.I不想放置Facebook登錄按鈕。我怎樣才能做到這一點如何獲取用戶的詳細信息上單擊按鈕不是在Facebook的按鈕在Android?

這裏是我的代碼: -

private Button loginButton; 
private CallbackManager callbackManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 
    setContentView(R.layout.activity_main); 

    loginButton = (Button) findViewById(R.id.login_button); 
    loginButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends")); 
     } 
    }); 
    LoginManager.getInstance().registerCallback(callbackManager, 
      new FacebookCallback<LoginResult>() { 
       @Override 
       public void onSuccess(final LoginResult loginResult) { 
        Log.d("Success", "Login"); 
        GraphRequest request = GraphRequest.newMeRequest(
          loginResult.getAccessToken(), 
          new GraphRequest.GraphJSONObjectCallback() { 
           @Override 
           public void onCompleted(JSONObject object, GraphResponse response) { 
            Log.e("JSON:", object.toString()); 
            String user_ID = null; 
            try { 
             user_ID = object.getString("id"); 
            } catch (JSONException e) { 
             e.printStackTrace(); 
            } 
            CFacbookStorage facebookstorage = new CFacbookStorage(); 
            facebookstorage.setM_UserId(user_ID); 
            Intent i = new Intent(MainActivity.this, CFacebookDeatails.class); 
            i.putExtra("user_data", facebookstorage); 
            startActivity(i); 
           } 
          }); 
        Bundle parameters = new Bundle(); 
        parameters.putString("fields", "id,name,email,gender, birthday"); 
        request.setParameters(parameters); 
        request.executeAsync(); 

       } 

       @Override 
       public void onCancel() { 
        // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void onError(FacebookException exception) { 
        Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      }); 
    AccessToken token; 
    token = AccessToken.getCurrentAccessToken(); 


    if (token == null) { 
     //Means user is not logged in 
    } else { 
     Log.d("TAG", "init: facebooklogin " + AccessToken.USER_ID_KEY + " " + AccessToken.ACCESS_TOKEN_KEY + " "); 
    } 
    loginButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends")); 

     } 
    }); 
} 


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

}

CFacbookStorage類代碼: -

public static CFacbookStorage cFacbookStorage; 

public String getM_UserId() { 
    return m_UserId; 
} 

public void setM_UserId(String m_UserId) { 
    this.m_UserId = m_UserId; 
} 

public String m_UserId; 

public static CFacbookStorage getInstance(){ 
    if (cFacbookStorage==null){ 
     cFacbookStorage = new CFacbookStorage(); 
    } 
    return cFacbookStorage; 
} 

}

CFacebookDetail

private TextView info; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog_details); 
    info = (TextView)findViewById(R.id.info); 

} 
這個代碼我收到錯誤loginButton.registerCallback

(){}

+0

你的意思是你想的Facebook登錄一氣呵成工作嗎? – Moulesh

+0

是................................................ ...... – Sandeep

+0

如果你正在從Facebook獲取信息......並且不想顯示Facebook登錄信息,而是顯示一些不同的按鈕,那麼你的道德上是不正確的......而Facebook不會讓你獲取信息,除非用戶同意允許你的應用程序訪問他們的基本信息...至少用戶會知道信息正從fb中獲取。因此,單擊並獲取配置文件信息是不可能的。 – Moulesh

回答

0

您可以通過重寫你的Facebook與使用的FrameLayout你的程序兼容性按鈕按鈕視圖實現這一目標。事情是這樣的:

  <FrameLayout 
       android:id="@+id/FrameLayout1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

      <com.facebook.login.widget.LoginButton 
       android:id="@+id/facebook" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/facebook" 
       android:layout_gravity="center_horizontal"/> 


      <ImageView 
       android:id="@+id/fb_login" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/facebook" 
       android:onClick="onClick" 
       android:layout_marginTop="10dp" 
       android:layout_gravity="center_horizontal"/> 

      </FrameLayout> 

的onClick實現:

public void onClick(View v) { 
     if (v == custom_fb_login) { 
      fb_login.performClick(); 
     } 
    } 

FbButton點擊:

private View.OnClickListener handleClick = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      switch (view.getId()) { 
      case R.id.facebook: 
        LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email")); 
        break; 
        } 
        } 
       } 
+0

在這裏看到的問題是,我不想要Facebook按鈕文本更改註銷始終保持在 – Sandeep

+0

你現在應該擔心,因爲你將使用framelayout作爲Facebook的原始按鈕將隱藏自己的Facebook按鈕圖像。你有沒有嘗試執行我的XML代碼? – Nidhi

+0

不能正常工作..... – Sandeep

1

使用您的按鈕XML文件,然後後的OnCreate下面的代碼活動中使用的:

private CallbackManager callbackManager; 

callbackManager = CallbackManager.Factory.create(); 

     LoginManager.getInstance().registerCallback(callbackManager, 
       new FacebookCallback<LoginResult>() { 
        @Override 
        public void onSuccess(final LoginResult loginResult) { 
         Log.d("Success", "Login"); 
         GraphRequest request = GraphRequest.newMeRequest(
           loginResult.getAccessToken(), 
           new GraphRequest.GraphJSONObjectCallback() { 
            @Override 
            public void onCompleted(
              JSONObject object, 
              GraphResponse response) { 
             // Application code 
             response.getError(); 
             Log.e("JSON:", object.toString()); 
            } 
           }); 
         Bundle parameters = new Bundle(); 
         parameters.putString("fields", "id,name,email,gender, birthday"); 
         request.setParameters(parameters); 
         request.executeAsync(); 

        } 

        @Override 
        public void onCancel() { 
         // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show(); 
        } 

        @Override 
        public void onError(FacebookException exception) { 
         Toast.makeText(SignInActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       }); 
     AccessToken token; 
     token = AccessToken.getCurrentAccessToken(); 


     if (token == null) { 
      //Means user is not logged in 
     } else { 
      Log.d("TAG", "init: facebooklogin " + AccessToken.USER_ID_KEY + " " + AccessToken.ACCESS_TOKEN_KEY + " "); 
     } 

廣告後d以下按鈕點擊事件代碼:

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        LoginManager.getInstance().logInWithReadPermissions(SignInActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends")); 

      } 
     }); 

的擺在那活動

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

但是如何在textview中獲取和顯示用戶標識等.... – Sandeep

+0

String user_id = object.getString(「id」)。toString(); String email = object.getString(「email」)。toString(); String name = object.getString(「name」)。toString(); String gender = object.getString(「gender」)。的toString(); –

+0

我這樣的: - 公共無效onCompleted(的JSONObject對象,GraphResponse響應){ \t \t \t \t \t \t \t \t \t \t嘗試{ \t \t \t \t \t \t \t \t \t \t \t字符串USER_ID = object.getString( 「ID」)的toString(); \t \t \t \t \t \t \t \t \t \t \t CFacbookStorage.getInstance()setM_UserId(USER_ID)。 \t \t \t \t \t \t \t \t \t \t \t意圖I =新意圖(MainActivity.this,CFacebookDeatails.class); \t \t \t \t \t \t \t \t \t \t \t startActivity(ⅰ); \t \t \t \t \t \t \t \t \t \t}趕上(JSONException E){ \t \t \t \t \t \t \t \t \t \t \t e.printStackTrace(); \t \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t \t \t //應用代碼 \t \t \t \t \t \t \t \t \t \t response.getError(); \t \t \t \t \t \t \t \t \t \t Log.e( 「JSON:」,object.toString()); \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t}); – Sandeep

相關問題