[我正在为我的应用程序编写我的登录页面的代码。我已经使用给定的电子邮件和密码使我的登录成功但我在编写用于登录facebook的代码时遇到了问题。我已经检查了这个https://developers.facebook.com/docs/facebook-login/android/v2.3
但是我仍然按照说明运行我的代码时遇到错误。我正在开发新的...所以任何适当的帮助都会值得称赞。提前付款。] 1
答案 0 :(得分:5)
如果你是什么customlogin按钮那么
在创建meathod
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create(); // declare it globally "CallbackManager callbackManager "
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>(){
@Override
public void onSuccess(LoginResult result)
{
// TODO Auto-generated method stub
Log.d("LoginManager", "On Success");
}
@Override
public void onError(FacebookException error) {
// TODO Auto-generated method stub
Log.d("LoginManager", "On Error");
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.d("LoginManager", "On Cancel");
}
});
在任何按钮上单击调用此
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile","user_friends","email"));
onActivity结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:5)
这里我提供了一个使用sdk4登录facebook的工作解决方案。
首先在build.gradle(Module app)下添加依赖项 依赖{......
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
}
然后同步Gradle ....
此后,在Android Manifest中进行以下更改: -
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id"/> //write the app_id u have generated in res/values/strings.xml folder.
<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" />
在此之后,在您的MainActivity类中编写以下代码:
public class MainActivity extends ActionBarActivity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email", "user_likes", "user_friends");
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//callback registration
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends"));
Log.e("-->", Arrays.asList("public_profile", "user_friends").toString());
Toast.makeText(getApplication(), "success", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// App code
Toast.makeText(getApplication(),"fail",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplication(),"error",Toast.LENGTH_SHORT).show();
}
});
}
});
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
我的activity_main.xml文件如下: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
您可以根据要求自定义按钮