I want to implement such a task: in the background (when app is in the background) look for new data for the user and give notifications about it. I have to do this periodically (every 2-3 seconds).There is different data so I use different methods for this (FetchImages ,FetchMessages, FetchNewConversations , etc.)As far as I know in Android 4 all network operations must be implemented in the backgroun thread, so I implement all this operations as classes inherited from AsyncTask and then to call them I just write FetchImages.execute () But activities execute only once so I decided to call them via ScheduledExecutorService like this:
.scheduleAtFixedRate(new Runnable() {
public void run()
{
FetchImages.execute(); //...
}
So the question is it a good idea or there exeist better and more efficient solution? Thanks in advance!
Since
ScheduledExecutorServicealready uses a background thread, you do not need to add another background thread viaAsyncTask. In fact, your code should crash, as you can only create anAsyncTaskon the main application thread.