我有一个使用firebase用户登录发送短信模板的应用。 我想在firbase数据库中保存每个用户的短信日志,这样我就可以看到每个用户发送的消息。我不知道如何制作它......
我的代码
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private ProgressBar spinner;
EditText EMAIL, PASSWORD;
Button SIGNIN;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
EMAIL = (EditText) findViewById(R.id.email);
PASSWORD = (EditText) findViewById(R.id.password);
SIGNIN = (Button) findViewById(R.id.login);
spinner = (ProgressBar) findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
SIGNIN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ((TextUtils.isEmpty(EMAIL.getText().toString())) && (TextUtils.isEmpty(PASSWORD.getText().toString()))) {
Toast.makeText(getApplicationContext(), "make sure that you enter full ogin info ", Toast.LENGTH_SHORT).show();
}else if (!isEmailValid(EMAIL.getText().toString())){
Toast.makeText(getApplicationContext(), "enter a valid mail", Toast.LENGTH_SHORT).show();
} else {
SignIn(EMAIL.getText().toString(), PASSWORD.getText().toString());
}
}
});
SIGNIN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pass = PASSWORD.getText().toString();
if(TextUtils.isEmpty(pass)) {
PASSWORD.setError("kindly enter password ");
return;
}
if ((TextUtils.isEmpty(EMAIL.getText().toString())) && (TextUtils.isEmpty(PASSWORD.getText().toString()))) {
Toast.makeText(getApplicationContext(), "wrong info", Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
} else {
spinner.setVisibility(View.VISIBLE);
SignIn(EMAIL.getText().toString(), PASSWORD.getText().toString());
}
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
}
};
mAuth.addAuthStateListener(mAuthListener);
}
private void SignIn(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
finish();
Toast.makeText(getApplicationContext(), "login done",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
} else {
Toast.makeText(getApplicationContext(), "Error username",
Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
}
});
}
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
}
sms.java
public class Harsh extends AppCompatActivity {
RadioButton lo, hi, mid;
String sense;
Button submit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.harsh_layout);
lo = (RadioButton) findViewById(R.id.lo_btn);
hi = (RadioButton) findViewById(R.id.hi_btn);
mid = (RadioButton) findViewById(R.id.mid_btn);
submit = (Button) findViewById(R.id.submitButton);
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String num = prefs.getString("nameKey", "0");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (lo.isChecked()) {
sendSMS( num, " message one");
} else if (hi.isChecked()) {
sendSMS( num, " message 2");
// for example save this message for current logged user
} else if (mid.isChecked()) {
sendSMS( num, " message 3");
}
Toast.makeText(getApplicationContext(), "Command Sent", Toast.LENGTH_LONG).show(); // print the value of selected super star
}
});
}
public void sendSMS(String phoneNo, String msg) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
Toast.makeText(getApplicationContext(), "Command Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
}
答案 0 :(得分:1)
经过大量的研究后,我发现set和get class,我修改了我的代码,如下所示
public class Post {
private String message;
private String number;
private String user;
private String date;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
然后在我需要将数据发布到firbase数据库的活动中我添加了那个简单的代码
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference posts = database.getReference("Messages:");
//this code for keep posts even app offline until the app online again
posts.keepSynced(true);
Post post = new Post();
post.setMessage("Messsage");
post.setUser(name);
post.setNumber(num);
post.setDate(s);
posts.push().setValue(post);