问题描述
我有以下的code轮询未读计数的通知从服务器每X秒
我开始通过的ScheduledThreadPoolExecutor在App.onCreate(这个过程)以及
Log.d(XXX,请求从服务器通知计数......);
被调用一次(我可以在logcat中看到的),但无论这两个改造的回调函数获取调用(事实上没有改造调试日志)。 Morever,在从服务器请求通知计数......是永远不会再次打印(即周期性任务不运行)
我使用改造为其他Web服务调用以及(根据用户输入),他们都工作正常(我可以看到的传入和传出请求/在logcat中反应)
公共类应用扩展应用{
私人ScheduledExecutorService的scheduleTaskExecutor;
...
@覆盖
公共无效的onCreate(){
super.onCreate();
//地区设立的定期通知计数侦听任务
scheduleTaskExecutor = Executors.newScheduledThreadPool(2);
scheduleTaskExecutor.scheduleAtFixedRate(新PeriodicNotifCountFetchTask(),0,5,TimeUnit.SECONDS);
// endregion
}
类PeriodicNotifCountFetchTask实现Runnable {
@覆盖
公共无效的run(){
Log.d(XXX,从服务器请求通知计数......);
。EMRestClient.getmEMRestService()getNotificationCount(新回拨< NotificationCount>(){
@覆盖
公共无效成功(NotificationCount响应,响应未使用的){
INT unreadNotifCount = response.getCount();
Log.d(XXX,成功获取通知的数量,未读=+ response.getCount());
如果(unreadNotifCount大于0){
//调用监听器重新绘制菜单
对于(NewNotificationListener X:notifListeners){
x.onNewNotificationReceived(response.getCount());
}
}
}
@覆盖
公共无效失败(RetrofitError错误){
Log.d(XXX,无法抓取的通知数从服务器);
}
});
}
}
}
的code的改造部分是在这里:
@POST(/通知/ notification_count /)
无效getNotificationCount(回拨< NotificationCount>回调);
我目前正在开发的机器人类似的事情。看看这可以帮助您:
的ScheduledThreadPoolExecutor执行人=新的ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(getReloadTask(位置),0,20,TimeUnit.SECONDS);
I have the following code for polling the unread notification count every X seconds from a server
I start the this process via ScheduledThreadPoolExecutor in the App.onCreate() and
Log.d("XXX", "Requesting Notification count from server ...");
is called once (i can see in Logcat), but neither of the two Retrofit call back functions getting called (and in fact no Retrofit debug logs). Morever, the "Requesting Notification count from server...." is never printed again (i.e. the periodic task is not running)
I am using Retrofit for other webservice calls as well (upon user input) and they are working fine (I can see the incoming and outgoing requests/responses in the logcat)
public class App extends Application {
private ScheduledExecutorService scheduleTaskExecutor;
...
@Override
public void onCreate() {
super.onCreate();
//region Set up the periodic notification count listener task
scheduleTaskExecutor= Executors.newScheduledThreadPool(2);
scheduleTaskExecutor.scheduleAtFixedRate(new PeriodicNotifCountFetchTask(), 0, 5, TimeUnit.SECONDS);
//endregion
}
class PeriodicNotifCountFetchTask implements Runnable {
@Override
public void run() {
Log.d("XXX", "Requesting Notification count from server ...");
EMRestClient.getmEMRestService().getNotificationCount(new Callback<NotificationCount>() {
@Override
public void success(NotificationCount response, Response unused) {
int unreadNotifCount = response.getCount();
Log.d("XXX", "Successfully fetched notification count, unread = " + response.getCount());
if (unreadNotifCount>0){
// call listener to repaint menu
for (NewNotificationListener x :notifListeners){
x.onNewNotificationReceived(response.getCount());
}
}
}
@Override
public void failure(RetrofitError error) {
Log.d("XXX", "Failed to fetch notification count from server");
}
});
}
}
}
The retrofit part of the code is here:
@POST("/notification/notification_count/")
void getNotificationCount(Callback<NotificationCount> callback);
I am currently developing similar things in android. See if this helps you:
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(getReloadTask(position), 0, 20, TimeUnit.SECONDS);
这篇关于的ScheduledThreadPoolExecutor的周期性任务(使用改装)刚烧制一次,永无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!