Run volley request every 5 minutes in background android

前端 未结 2 1977
盖世英雄少女心
盖世英雄少女心 2021-02-04 21:52

I use Volley library to connect with server in my app. Now, I have to send request in background every 5 minutes also when app is not running (killed by user). How should I do i

2条回答
  •  独厮守ぢ
    2021-02-04 22:12

    I prefer to use Android Handler because it is executes in UI Thread by default.

    import android.os.Handler;
    
    // Create the Handler object (on the main thread by default)
    Handler handler = new Handler();
    // Define the code block to be executed
    private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
    
           sendVolleyRequestToServer(); // Volley Request 
    
          // Repeat this the same runnable code block again another 2 seconds
          handler.postDelayed(runnableCode, 2000);
        }
    };
    // Start the initial runnable task by posting through the handler
    handler.post(runnableCode);
    

提交回复
热议问题