Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run volley request every 5 minutes in background android

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 it? With background services, AlarmManager (Google says that it isn't good choice for network operations) or something else?

Or maybe SyncAdapter will be good for it?

like image 242
Seravier Avatar asked Aug 27 '15 20:08

Seravier


People also ask

Is Android volley deprecated?

GitHub - mcxiaoke/android-volley: DEPRECATED.

Does volley run in background thread?

Every network request performed by Volley is performed in a background thread. Volley takes care of this behind the scenes. So there is no need to perform a request on a different thread, since that's already happening. The listeners, on the other hand, are called on the UI thread.

How do I make my volley request synchronous?

A volley class used for blocking requests. To access the get() method and make an Android Volley synchronous request, you need to use the RequestFuture class instead of standard StringRequest or JsonObjectRequest class. This RequestFuture class also implements both Response. Listener and Response.

What is Android volley example?

Volley Uses Caches To Improve Performance In Android: For example, lets say you are using Asynctask to fetch image and description from a JSON array created in server API. The content is fetched in the portrait mode and now user rotate screen to change it to landscape mode.


2 Answers

You can use a TimerTask with scheduleAtFixedRate in a service class to achieve this, here is an example of Service class, you can use it

public class ScheduledService extends Service 
{

private Timer timer = new Timer();


@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

@Override
public void onCreate() 
{
    super.onCreate();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            sendRequestToServer();   //Your code here
        }
    }, 0, 5*60*1000);//5 Minutes
}

@Override
public void onDestroy() 
{
    super.onDestroy();
}

}

You can use sendRequestToServer method to connect with the server. Here is the manifest declaration of the Service.

<service android:name=".ScheduledService" android:icon="@drawable/icon" android:label="@string/app_name" android:enabled="true"/>

To start the service from MainActivity,

// use this to start and trigger a service
Intent i= new Intent(context, ScheduledService.class);
context.startService(i);
like image 193
Arsal Imam Avatar answered Oct 22 '22 00:10

Arsal Imam


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);
like image 44
Umanda Avatar answered Oct 22 '22 01:10

Umanda