FB登录Android

时间:2015-07-06 06:45:06

标签: android facebook login

执行以下操作后,将调用OnCancel方法:

<activity
            android:name=".FbLoginActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_fb_login"
            android:theme="@style/FullscreenTheme" >
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="xxxxxxxxxxxxxx"/>
        <provider android:authorities="com.facebook.app.FacebookContentProvider456579934439214"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />


        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />

在活动中:

public class FbLoginActivity extends Activity {

    TextView tvTermsLink;
    CallbackManager callbackManager;
    String TAG="FbLoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_fb_login);
        LoginButton loginButton = (LoginButton) findViewById(R.id.authButton);

        loginButton.setReadPermissions(Arrays.asList("user_location", "email", "user_friends", "user_birthday", "user_likes", "user_work_history", "user_relationships", "user_relationship_details",
                "user_education_history"));


        tvTermsLink=(TextView) findViewById(R.id.fbl_tv_termsLink);
        tvTermsLink.setText(Html.fromHtml("By logging in you accept the <a href=\"http://crownit.in/terms\">Terms & Conditions</a> "));

        tvTermsLink.setMovementMethod(LinkMovementMethod.getInstance());




        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

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

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException e) {

                }
            });


            }
        });

    }

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

在长时间推荐在线资源后,我无法理解代码中的错误。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Facebook的默认登录按钮,则无需设置onclick事件。你可以像这样使用它。

&#13;
&#13;
loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
	"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
	public void onSuccess(LoginResult loginResult) {

		System.out.println("onSuccess");

		String accessToken = loginResult.getAccessToken()
			.getToken();
		Log.i("accessToken", accessToken);

		GraphRequest request = GraphRequest.newMeRequest(
		loginResult.getAccessToken(),
		new GraphRequest.GraphJSONObjectCallback() {@Override
			public void onCompleted(JSONObject object,
			GraphResponse response) {
				Log.i("LoginActivity", response.toString());
				try {
					id = object.getString("id");
					try {
						URL profile_pic = new URL(
							"http://graph.facebook.com/" + id + "/picture?type=large");
						Log.i("profile_pic",
						profile_pic + "");

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					String name = object.getString("name");
					String email = object.getString("email");
					String gender = object.getString("gender");
					String birthday = object.getString("birthday");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		Bundle parameters = new Bundle();
		parameters.putString("fields",
			"id,name,email,gender, birthday");
		request.setParameters(parameters);
		request.executeAsync();
	}

	@Override
	public void onCancel() {
		System.out.println("onCancel");
	}

	@Override
	public void onError(FacebookException exception) {
		System.out.println("onError");
		Log.v("LoginActivity", exception.getCause().toString());
	}
});
&#13;
&#13;
&#13;

如果您使用自定义的Facebook按钮,而不仅仅是在facebook login with customized button

检查我的答案