I want to repeatedly fetch users from database in specific intervals and then do something with them on UI. I know that asynchTask can only be executed once but I want to it to run repeatedly to fetch fresh users' data. I am wondering is there any way to do this, i.e. fetch users after a specific interval repeatedly and then do something with them on UI ??? Is this valid code..
while(true) {
new plottingTask().execute("");
Thread.sleep(10000);
}
Check this question: How to execute Async task repeatedly after fixed time intervals
It has already been asked several times.
A infinite loop is not a good way to go!
If you want to fetch data even if your Activity
is in background/or not running, a Service
would be the better way.
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
MyAsyncTask MyTask = new MyAsyncTask ();
MyTask.execute();
return Service.START_STICKY; // if android our the user kills the Service it will restart
}
}
private class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// do your long/network operation
}
@Override
protected void onPostExecute(String result) {
// send Intent to BroadcastReceiver in MainActivity.class to know the service finished its task once
Intent finished = new Intent("yourPackage.DOWNLOADCOMPLETE");
sendBroadcast(finished);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
In your MainActivity add a BroadcastReceiver:
@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new ResponseReceiver();
registerReceiver(receiver, filter);
}
// notify when operation in Service finished
private ResponseReceiver receiver;
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "yourPackage.DOWNLOADCOMPLETE";
@Override
public void onReceive(Context context, Intent intent) {
// do stuff if Service finished its task at least once
}
}
Start the Servive every 60 seconds using AlarmManager:
// refresh every 60 seconds in MyService.java
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// Start every 60 seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
startService(intent);
In your AndroidManifest.xml you have to register the Service:
<service
android:enabled="true"
android:name=".MyService"
android:label="your Service Name">
</service>
android:label="your Service Name"
will be displayed if you navigate to running apps in android.