所以我打算将登录facebook的信息存储到getter和setter,以便我可以发布它可以从BroadcastFragment.java发布到我的Firebase数据库中。我不明白为什么它总是返回null。我在FacebookLogin.java中放置我的二传手的错误是什么?我的User.java有问题吗?
FacebookLogin.java
public class FacebookLogin extends AppCompatActivity{
private static final String TAG = "FacebookLogin";
private LoginButton loginButton;
private CallbackManager callbackManager;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
public DatabaseReference mRootRef;
public Firebase mRef;
private ProgressBar progressBar;
private final String uid = getUid();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.facebook_login);
Firebase.setAndroidContext(this);
mRootRef = FirebaseDatabase.getInstance().getReference();
mRef = new Firebase("https://peoplehelper-c5a97.firebaseio.com/users");
progressBar = (ProgressBar) findViewById(R.id.progressBar);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.loginButton);
loginButton.setReadPermissions(Arrays.asList("email", "public_profile"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
Log.d("", "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel()
{Toast.LENGTH_SHORT).show();
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error)
{
//Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
Log.d(TAG, "facebook:onError", error);
}
});
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuthListener = new FirebaseAuth.AuthStateListener()
{
@Override
public void onAuthStateChanged(@NotNull FirebaseAuth firebaseAuth)
{
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null)
{
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
}
else
{
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
}
private void handleFacebookAccessToken(AccessToken accessToken)
{
Log.d(TAG, "handleFacebookAccessToken:" + accessToken);
progressBar.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.GONE);
final FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();
/**String uid = users.getUid();*/
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
public String facebookUserId;
String uid = users.getUid();
@Override
public void onComplete(@NotNull Task<AuthResult> task)
{
if (!task.isSuccessful())
{
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(FacebookLogin.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "Authentication Failed", Toast.LENGTH_SHORT).show();
// Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
}
else
{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
Profile profile = Profile.getCurrentProfile();
facebookUserId = profile.getId();
String facebookId=facebookUserId;
User users = new User(uid,name,email,facebookId);
users.setUserId(uid);
users.setUserName(name);
users.setUserEmail(email);
// mEmailRef = mRootRef.child("users").child(userId).child("email”)
// Firebase mRefChild = mRef.child("Name").child(uid).child("email");
mRef.child(uid).setValue(users);
Intent intent = new Intent(getApplicationContext() ,Home.class);
intent.putExtra("user_id",uid);
intent.putExtra("profile_picture",image);
startActivity(intent);
finish();
goMainScreen();
}
progressBar.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.GONE);
}
});
}
private void goMainScreen()
{
Intent intent = new Intent (this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onStart()
{
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
}
@Override
protected void onStop()
{
super.onStop();
firebaseAuth.removeAuthStateListener(firebaseAuthListener);
}
public String getUid()
{
return uid;
}
}
BroadcastFragment.java
public class BroadcastFragment extends Fragment implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener{
int day, month, year, hour, minute;
int dayFinal, monthFinal, yearFinal, hourFinal, minuteFinal;
Button deadline;
EditText reqdesc;
Button post;
public String dead="", role="requester", r;
DatabaseReference dr;
String uid="", uname="", uemail="";
User u = new User();
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.broadcast, container, false);
dr = FirebaseDatabase.getInstance().getReference("BroadcastRequest");
reqdesc = (EditText) myView.findViewById(R.id.reqdesc);
deadline = (Button) myView.findViewById(R.id.deadline);
deadline.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), BroadcastFragment.this,
year, month, day);
datePickerDialog.show();
}
});
post = (Button) myView.findViewById(R.id.post);
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dead = deadline.getText().toString();
r = reqdesc.getText().toString();
uid = u.UserId();
uname = u.UserName();
uemail = u.UserEmail();
writeNewRequest(r, dead, uemail, uid, uname, role, "Siquijor");
showNotification();
}
});
return myView;
}
private void writeNewRequest(String r, String dead, String email, String id, String name, String role, String cl) {
DatabaseReference post = dr.push();
String date = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
post.child("RequestDeadline").setValue(dead);
post.child("RequestDescription").setValue(r);
post.child("email").setValue(email);
post.child("id").setValue(id);
post.child("name").setValue(name);
post.child("role").setValue(role);
post.child("CurrentDateTime").setValue(date);
post.child("CurrentLocation").setValue(cl);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
yearFinal = year;
monthFinal = month;
dayFinal = dayOfMonth;
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), BroadcastFragment.this,
hour, minute, DateFormat.is24HourFormat(getActivity()));
timePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hourFinal = hourOfDay;
minuteFinal = minute;
String monthname = monthName(monthFinal);
String time = midnight(hourFinal,minuteFinal);
String dl = monthname+" "+dayFinal+" "+yearFinal+" "+time;
deadline.setText(dl);
}
private String midnight(int h, int m){
String mid="", hour="";
if(h<=11){
mid = "AM";
if(h==00)
h=12;
}
else if(h>=12){
mid = "PM";
if(h>=13)
h-=12;
}
hour = number(h);
String minute = number(m);
String t = hour+":"+minute+mid;
return t;
}
private String number(int n){
String a = Integer.toString(n);
if(n<=9){
a = "0"+a;
return a;
}
else
return a;
}
private String monthName(int month){
month+=1;
String mn="";
switch (month){
case 1: mn="January";
break;
case 2: mn="February";
break;
case 3: mn="March";
break;
case 4: mn="April";
break;
case 5: mn="May";
break;
case 6: mn="June";
break;
case 7: mn="July";
break;
case 8: mn="August";
break;
case 9: mn="September";
break;
case 10: mn="October";
break;
case 11: mn="November";
break;
case 12: mn="December";
break;
}
return mn;
}
public void showNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("PEOPLE HELPER");
builder.setContentText("Your request has been broadcasted");
Intent intent = new Intent(getActivity(), BroadcastFragment.class); //creates an explicit intent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());
stackBuilder.addParentStack(getActivity()); //adds the intent
stackBuilder.addNextIntent(intent); //put the intent to the top of the stack
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); //(id, flag) //creates a pending intent
builder.setContentIntent(pendingIntent); //adds the PendingIntent to the builder
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
User.java
public class User {
public String uid;
public String name;
public String email;
public String facebookId;
public User(){
}
public User(String uid, String name, String email, String facebookId)
{
this.uid = uid;
this.name = name;
this.email = email;
this.facebookId = facebookId;
}
public void setUserId(String uid)
{
this.uid = uid;
}
public void setUserName(String name){
this.name = name;
}
public void setUserEmail(String email){
this.email = email;
}
public String UserId(){
return uid;
}
public String UserName(){
return name;
}
public String UserEmail(){
return email;
}
}