本文介绍了FB登陆安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实施以下后,OnCancel的方法被称为:

 <活动
            机器人:名称=。FbLoginActivity
            机器人:configChanges =定位| keyboardHidden |屏幕尺寸
            机器人:标签=@字符串/ title_activity_fb_login
            机器人:主题=@风格/ FullscreenTheme>
        < /活性GT;

        &所述;元数据
            机器人:名称=com.facebook.sdk.ApplicationId
            机器人:值=XXXXXXXXXXXXXX/>
        <提供安卓当局=com.facebook.app.FacebookContentProvider456579934439214
            机器人:名称=com.facebook.FacebookContentProvider
            机器人:出口=真/>


        <活动机器人:名称=com.facebook.FacebookActivity
            机器人:configChanges =
                键盘| keyboardHidden |屏幕布置|屏幕尺寸|定位
            机器人:主题=@安卓风格/ Theme.Translucent.NoTitleBar
            机器人:标签=@字符串/ APP_NAME/>
 

在活动:

 公共类FbLoginActivity延伸活动{

    TextView的tvTermsLink;
    CallbackManager callbackManager;
    字符串变量=FbLoginActivity;
    @覆盖
    保护无效的onCreate(包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,电子邮件,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(通过登录您接受的< A HREF = \的http://crownit.in/terms \>条款及细则< / A>中));

        tvTermsLink.setMovementMethod(LinkMovementMethod.getInstance());




        loginButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图查看){

              。LoginManager.getInstance()registerCallback(callbackManager,新FacebookCallback< LoginResult>(){
                @覆盖
                公共无效的onSuccess(LoginResult loginResult){

                }

                @覆盖
                公共无效OnCancel的(){

                }

                @覆盖
                公共无效onerror的(FacebookException E){

                }
            });


            }
        });

    }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,因此code,数据);
        callbackManager.onActivityResult(要求code,因此code,数据);
    }
 

延长提到的在线资源后,我无法理解什么是错在code。任何帮助将AP preciated。

解决方案

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

loginButton =(LoginButton)findViewById(R.id.login_button);名单<字符串> permissionNeeds = Arrays.asList(user_photos,电子邮件,user_birthday,public_profile,AccessToken);loginButton.registerCallback(callbackManager,新FacebookCallback< LoginResult> (){@覆盖公共无效的onSuccess(LoginResult loginResult){的System.out.println(的onSuccess);字符串accessToken = loginResult.getAccessToken().getToken();Log.i(accessToken,accessToken);GraphRequest请求= GraphRequest.newMeRequest(loginResult.getAccessToken(),新GraphRequest.GraphJSONObjectCallback(){@覆盖公共无效onCompleted(的JSONObject对象,GraphResponse响应){Log.i(LoginActivity,response.toString());尝试 {ID = object.getString(ID);尝试 {URL profile_pic =新的URL(http://graph.facebook.com/+编号+?/图像类型=大);Log.i(profile_picprofile_pic +);}赶上(MalformedURLException异常E){e.printStackTrace();}字符串名称= object.getString(姓名);字符串email = object.getString(电子邮件);字符串性别= object.getString(性别);字符串的生日= object.getString(生日);}赶上(JSONException E){e.printStackTrace();}}});捆绑参数=新包();parameters.putString(域,编号,姓名,电子邮箱,性别,生日);request.setParameters(参数);request.executeAsync();}@覆盖公共无效OnCancel的(){的System.out.println(OnCancel的」);}@覆盖公共无效onerror的(FacebookException除外){的System.out.println(onError的);Log.v(LoginActivity,exception.getCause()的toString());}});

如果您使用自定义的Facebook按钮,不仅仅是这里facebook使用自定义按钮登录

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.

解决方案

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

if you using customized facebook button than just check my answer here facebook login with customized button

这篇关于FB登陆安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:29