线程完成其工作或在android中返回值后,在MainActivity中运行一行

我正在线程在MainActivity中完成其任务之后尝试运行一行代码,但是该行甚至在线程完成其任务之前就运行了。

new PostUsernameAndPassword(username,password, UserAuthenticationFailAlert);

//this line is running before the value is set in the thread!!
if(UserAuthenticationFailAlert.getText().toString().equals("Access granted")){
//do something
}           

如何在android中执行类似于function.then()的操作?

评论
awzse
awzse

You can use callbacks. Pass a callback function to your PostUsernameAndPassword as a param, and then call it AFTER the job is done.

https://www.geeksforgeeks.org/asynchronous-synchronous-callbacks-java/

点赞
评论