我有一个Service,其中在特定时间间隔后,我正在调用API,甚至每次都调用API,但是新添加的内容不会被更新。我找不到发生这种情况的任何原因。
假设每隔1分钟就会调用一次API,并返回相同的数据。一旦我关闭并重新启动服务,数据就会更新。
编辑
public class MyCustomService extends Service {
public int counter = 0;
Context context;
private List<List<Articles>> articlesList = null;
private List<Articles> articlesListChild = new ArrayList<>();
private List<Articles> articlesListChildNew = new ArrayList<>();
public MyCustomService(Context applicationContext) {
super();
context = applicationContext;
}
public MyCustomService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
context = this;
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
startTimer();
}
private Timer timer;
private TimerTask timerTask;
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 10*1000, 10*1000);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
checkForNewArticles();
}
};
}
public void checkForNewArticles() {
Api.getClient().getHomeScreenContents(3, new Callback<ArticlesResponse>() {
@Override
public void success(ArticlesResponse articlesResponse, Response response) {
articlesList = articlesResponse.getHomeScreenData();
for (int i = 0; i < articlesList.size(); i++) {
articlesListChildNew = articlesList.get(i);
articlesListChild.addAll(articlesListChildNew);
}
System.out.println("ARTICLE 1 : " + articlesListChild.get(0).getArticleHeading());
System.out.println("ARTICLE 2 : " + articlesListChild.get(1).getArticleHeading());
System.out.println("ARTICLE 3 : " + articlesListChild.get(2).getArticleHeading());
}
@Override
public void failure(RetrofitError error) {
Log.e("erroronlsArticles", error.toString());
}
});
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}