Reputation: 1142
I am writing an Android application where I need to periodically (every 30 seconds) fetch data from the server. I was thinking about using AlarmManager
to schedule those tasks, however I need to be able to pass a callback function that updates the view. Since the Intent
cannot encapsulate a callback, I decided to find another solution, which was a "worker thread". The problem here is that the request to the server is performed in AsyncTask
where onPreExecute
and onPostExecute
have to be running from the Ui thread, so I really cannot do it this way either.
I would appreciate any suggestions what would be the best approach in this case.
Upvotes: 1
Views: 1129
Reputation: 22527
Use a Handler
.
Handler mHandler;
public void useHandler() {
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 30000);
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
Log.e("Handlers", "Call asynctask");
/** Call your AsyncTask here **/
mHandler.postDelayed(mRunnable, 30000);
}
};
Upvotes: 2