我是应用开发的新手(Android Studio)。 我有两个类,一个是main类,另一个是连接到firebase的类(数据库),它检查用户是否存在并分别回答true或false并打印一些语句。 问题是即使任务成功并且返回false,android studio也不会等待响应。 我应该进入logcat的打印语句应该是 “这里1”,“这里2”,然后“这里3”。但这给了我“这里2”,“这里3”,然后是“这里1”。
我知道它必须执行线程操作或类似操作。
public class loginpage extends AppCompatActivity {
private Button Login;
private EditText user_email_txt;
private EditText user_password_txt;
private ProgressBar wait_bar;
private String[] user_name_password ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find_all_views();
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getuser_login_details();
disable_all_views();
Activate_wait_bar();
final Database login = new Database();
boolean decision = login.sign_in(user_name_password[0], user_name_password[1]);
System.out.println("here 3");
}
});
}
private void find_all_views() {
user_email_txt = findViewById(R.id.UserName);
user_password_txt = findViewById(R.id.Password);
Login = findViewById(R.id.submit_button);
wait_bar = findViewById(R.id.wait_bar);
}
private void getuser_login_details() {
user_name_password = new String[2];
user_name_password[0] = user_email_txt.getText().toString();
user_name_password[1] = user_password_txt.getText().toString();
}
private void disable_all_views() {
user_email_txt.setVisibility(View.INVISIBLE);
user_password_txt.setVisibility(View.INVISIBLE);
Login.setVisibility(View.INVISIBLE);
}
private void Activate_wait_bar() {
wait_bar.setVisibility(View.VISIBLE);
}
}
public class Database {
private FirebaseAuth connect_databse;
private boolean authorisation;
public Database() {}
public boolean sign_in(String user_name, String Password) {
authorisation = false;
connect_databse = FirebaseAuth.getInstance();
connect_databse.signInWithEmailAndPassword(user_name, Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = connect_databse.getCurrentUser();
authorisation = true;
System.out.println("here 1");
}
}
});
System.out.println("here 2");
return authorisation;
}
}
如果任务成功,则应按打印语句“ here 1”,“ here 2”和“ here 3”的顺序返回authorization = true。
答案 0 :(得分:0)
创建一个新文件:
interface OnCompleteListener {
void onComplete();
}
在数据库类中:
public class Database {
private FirebaseAuth connect_databse;
private boolean authorisation;
public Database() {
}
public void sign_in(String user_name, String Password,final OnCompleteListener listener) {
authorisation = false;
connect_databse = FirebaseAuth.getInstance();
connect_databse.signInWithEmailAndPassword(user_name, Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = connect_databse.getCurrentUser();
authorisation = true;
System.out.println("here 1");
listener.onComplete();
System.out.println("here 2");
}
}
});
}
活动中:
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getuser_login_details();
disable_all_views();
Activate_wait_bar();
final Database login = new Database();
boolean decision = login.sign_in(user_name_password[0], user_name_password[1], new OnCompleteListener{
@Override
public void onComplete(){
System.out.println("here 3");
}
});
}
});
答案 1 :(得分:0)
使用网络连接或调用API从服务器获取数据时,必须使用后台线程,并且在完成任务后,将运行OnComplete
中的代码。但是主线程不会停止执行您的任务,而是依次从OnComplete
方法中运行所有代码。
在这种情况下,您可以使用CallBackMethod
。