My application should update a list of data coming from the server every 5 minutes. Using Retrofit for HTTP
request, what is the best way to do this?
My application should update a list of data coming from the server every 5 minutes. Using Retrofit for HTTP
request, what is the best way to do this?
You can use Android's Alarm feature and set how long how long it should be calling .
First you need to create a broadcast receiver that will be called when the alarm is run:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = AlarmReceiver.class.getSimpleName();
public AlarmReceiver() {
}
@Override
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, "onReceive()");
// Aqui você coloca o código que deve ser executado a cada X minutos
}
}
Do not forget to add the receiver to your AndroidManifest.xml file (within the application markup):
<receiver
android:name=".AlarmReceiver"
android:exported="false"></receiver>
Now you need to create a custom class that extends the Application class (if you do not already have it in your app). I recommend you create the alarm within the onCreate method:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, pendingIntent);
}
}
Now just add the custom class in AndroidManifest.xml :
<application
android:name=".MyApplication"
Add the WAKE_LOCK permission on AndroidManifest.xml :
<uses-permission android:name="android.permission.WAKE_LOCK" />
A timer is easier to implement:
private final Timer myTimer = new Timer();
public class myClass {
@Override
public void onResume() {
...
myTimer.scheduleAtFixedRate(new myTask(), 0, (long) (18000000)); // Tempo em milisegundos.
}
private class myTask extends TimerTask {
@Override
public void run() {
... execute os procedimentos.
}
}
}
There are several ways to do this, before choosing one, it is necessary to know what each one offers us and choose the one that best suits our needs.
The main advantage of Alarm is that it operates outside the context of your application, allowing you to make the request, even if the application is not running. However, it can also be the biggest disadvantage. After you start Alarm it will make a request every 5 minutes while the device is not turned off, with corresponding battery and resource costs.
If you only need to update the list while the application is running, do not use an Alarm .
In this situation the use of Timer could be a possible option.
However, the code run by Timer runs in a background thread , as Retrofit also runs its methods in a Thread itself, a background thread to run another background thread .
In addition, I would have to solve the problem of the onResponse()
and onFailure()
methods being executed in the thread created by Timer and not in the UIThread .
On the other hand the Timer should not be used in time consuming tasks.
So, if you just need to update the list while the application is running, I suggest you use a Handler .
Declare an attribute to save the reference to Handler
private Handler handler;
Create a Handler and immediately make the request:
handler = new Handler();
handler.post(new UpdateData());
To avoid memory leaks declare the UpdateData class in a separate java file or as a static inner Activity class.
private static final class UpdateData implements Runnable{
@Override
public void run() {
//Faça aqui a requisição via Retrofit
.....
//agenda nova requisição daqui a 5 minutos
handler.postDelayed(this, 5*60*1000);
}
}
In the onCreate()
method, remove the request for new requests:
@Override
protected void onDestroy(){
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}