Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

facebook - FB login Android

After implementing the following , OnCancel method is being called :

<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" />

In the Activity:

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

After prolonged referring the online resources, I am not able to understand whats wrong in the code. Any help will be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using facebook's default login button than no need to set onclick event. you can use it like this.

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());
}
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...