Reputation: 11
I am trying to show a waiting screen in my app while it's trying to connect my server. For some reason, the screen shows only in the middle of the while loop and sometimes even after the while loop has done its work. Also, even after the while loop gets to its end the code after it doesn't executed (it doesn't print the values). Does anyone know how to fix it?
This is the code:
public class LoadingActivity extends AppCompatActivity {
public static final long SEC_IN_MS = 1000;
public static final int CONNECT_LOOP_TIME = 5; //In seconds
DataSender dataSender = new DataSender();
DataCenter dataCenter = DataCenter.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
dataSender.execute();
dataCenter.connect();
}
@Override
protected void onResume() {
super.onResume();
moveToMenu();
}
public void moveToMenu() {
long loopStartTime = System.currentTimeMillis();
boolean didConnect = false;
while(!didConnect &&
System.currentTimeMillis()-loopStartTime <= SEC_IN_MS*CONNECT_LOOP_TIME) {
//Wait until connected or 5 seconds pass
System.out.println(System.currentTimeMillis()-loopStartTime);
if(dataCenter.getRespond().equals(dataCenter.okMsg())) {
didConnect = true;
}
}
System.out.println("111111111111111111");
System.out.println(didConnect);
if(didConnect) {
Intent nextIntent = new Intent(this, MenuActivity.class);
startActivity(nextIntent);
}
else {
System.out.println("OOPS");
}
}
}
Upvotes: 0
Views: 103