我在我的项目中做了Facebook登录。它在活动中效果很好。我只是尝试添加片段。因为,登录后我希望用户可以logout
和Facebook button
一起使用此片段中的内容。但是Facebook登录button
不会出现在模拟器中。怎么解决呢?
AccountFragment.java
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
public class AccountFragment extends Fragment {
private Button buttonLogout, buttonAccount;
// Facebook login için
LoginButton buttonFbLogin;
private CallbackManager callbackManager;
public AccountFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonFbLogin = view.findViewById(R.id.fb_button); //Facebook button
buttonLogout=view.findViewById(R.id.button_acc_Profile_Logout); // uygulamanın Logout butonu
callbackManager = CallbackManager.Factory.create(); // FB için
buttonFbLogin.setReadPermissions(Arrays.asList("email","public_profile")); //FB için
buttonFbLogin.setFragment(this);
checkFacebookLoginStatus();
//facebook buton tıklandığı zaman yapılacak login için
buttonFbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
//Facebook için
private void checkFacebookLoginStatus() {
if(AccessToken.getCurrentAccessToken()!=null)
{
buttonFbLogin.setVisibility(View.VISIBLE);
buttonLogout.setVisibility(View.GONE);
loadUserProfile(AccessToken.getCurrentAccessToken());
}
}
// Facebook için (Giriş-Çıkış için) //////
AccessTokenTracker tokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken)
{
if(currentAccessToken==null)
{
// txtName.setText("");
// txtEmail.setText("");
/// circleImageView.setImageResource(0);
deleteCartTable();
deleteShoppingCart();
deleteShPrefencesLogin();
Intent intent = new Intent(getContext(),MainActivity.class);
startActivity(intent);
Toast.makeText(getContext(),"User Logged out",Toast.LENGTH_LONG).show();
}
else
loadUserProfile(currentAccessToken);
}
};
// Facebook için End //////
private void loadUserProfile(AccessToken newAccessToken)
{
GraphRequest request = GraphRequest.newMeRequest(newAccessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
try {
String id = object.getString("id");
String image_url = "https://graph.facebook.com/"+id+ "/picture?type=normal";
RequestOptions requestOptions = new RequestOptions();
requestOptions.dontAnimate();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields","first_name,last_name,email,id");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode,resultCode,data);
super.onActivityResult(requestCode, resultCode, data);}
}
fragment_account_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:clickable="true"
android:focusable="true"
tools:context=".Controllers.AccountFragment">
<com.facebook.login.widget.LoginButton
android:id="@+id/fb_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginEnd="200dp"
android:layout_marginBottom="382dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_acc_Profile_Logout" />
</android.support.constraint.ConstraintLayout>