Home > Mobile >  Android emulator shows gray screen
Android emulator shows gray screen

Time:03-09

I'm trying to make a function that makes api calls to the server each 30 seconds and updates UI each time with fresh data. I made an infinite loop, where I iterate through array with fixed number of endpoints, and inside this loop I call my function. But when I run the app on emulator, the app shows nothing, just a dark-grey background:

emulator

It also doesn't logs any error, so I have no clue what can be the issue. Below is the code for infinite loop:

private String [] endpoints = new String[] {"https://server.com/api/update-rates?locale=en", 
                                            "https://server.com/api/update-rates?locale=fr", 
                                             "https://server.com/api/update-rates?locale=gr"};
    Log.d("forloop", "test1");
    while(true) {
        Log.d("forloop", "test2");
        for (int i = 0; i < endpoints.length; i  ) {
            getFreshRates(endpoints[i]);
            Log.d("forloop", String.valueOf(i));
            try {
                TimeUnit.SECONDS.sleep(30);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
            }
            if(i == 2) {
                i = -1;
            }
        }
    }  

I guarantee that getFreshRates method is works properly (I've tested with static url) so the issue might be with this loop or some android specific issue.

The Log.d output are:

2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test1
2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test2
2022-03-04 10:56:16.270 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:56:46.279 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:57:16.281 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:57:46.288 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:58:16.295 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:58:46.304 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:59:16.310 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:59:46.317 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 11:00:16.320 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 11:00:46.332 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 11:01:16.341 8359-8359/com.example.currencyrates D/forloop: 1  
.....
.....
.....  
// and so on, so the for loop works as expected too  

As I said, once I remove this while loop and call a function with static url, everything works perfect, but the thing is I need to infinitely call every 30 seconds with different endpoint. What could be the issue ?

CodePudding user response:

I've managed to solve it by using Handlers

// Create the Handler
private Handler handler = new Handler();

    @Override
protected void onCreate(Bundle savedInstanceState) {  
    // Start the Runnable immediately
    handler.post(runnable);  
}   

static int i = 0;
// Define the code block to be executed
private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        Log.d("statici", String.valueOf(i));
        getFreshRates(endpoints[i]);
            i  ;
            if(i == 3) {
                i = 0;
            }
        // Repeat every 30 seconds
        handler.postDelayed(runnable, 30000);
    }
};  
  
  • Related