我开发了一项服务,其中包括计时器任务,每5分钟运行一次,用于保存设备的跟踪记录,每五分钟就会向数据库添加一条记录。
当手机不动时,我的服务工作正常,即它应该每5分钟后给出一次记录。但我注意到,当手机处于移动状态时,它会在10或20分钟后更新点数,即每当用户在移动时停止使用。
如果是的话,服务会在移动中冻结! whatsapp messenger是如何管理它的? 请帮忙! 我正在编写我的onstart方法。请帮忙
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
mLocationClient.connect();
final Handler handler_service = new Handler();
timer_service = new Timer();
TimerTask thread_service = new TimerTask() {
@Override
public void run() {
handler_service.post(new Runnable() {
@Override
public void run() {
try {
some function of tracking
}
});
}
};
timer_service.schedule(thread_service, 1000, service_timing);
//sync thread
final Handler handler_sync = new Handler();
timer_sync = new Timer();
TimerTask thread_sync = new TimerTask() {
@Override
public void run() {
handler_sync.post(new Runnable() {
@Override
public void run() {
try {
//connecting to the central server for updation
Connect();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer_sync.schedule(thread_sync,2000, sync_timing);
}