本文介绍了如何在服务内添加天文钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在服务中添加天文钟"并获取值.但是我没有得到正确的天文钟"值.
I am triying to add Chronometer inside service and fetch value.But i am not getting correct Chronometer value.
public class NewLocationUpdateService extends Service {
Chronometer chronometer;
private static final int LOC_API_CALL_INTERVAL = 60 * 1000;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
chronometer = new Chronometer(NewLocationUpdateService.this);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startTimer();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
stopTimer();
}
//Timer related functions
private void startTimer(){
if(timer!=null ){
return;
}
timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
long millis=SystemClock.elapsedRealtime() - chronometer.getBase();
long seconds = (millis / 1000) % 60;
Log.e("timefortest", "" + seconds);
}
}, LOC_API_CALL_INTERVAL, LOC_API_CALL_INTERVAL);
}
private void stopTimer(){
if(null!=timer){
timer.cancel();
timer=null;
}
}
}
我得到的输出
E/timefortest: 7
E/timefortest: 1
E/timefortest: 0
E/timefortest: 5
E/timefortest: 0
E/timefortest: 0
E/timefortest: 0
E/timefortest: 0
E/timefortest: 1
E/timefortest: 0
E/timefortest: 2
我期待
E/timefortest: 60
E/timefortest: 120
E/timefortest: 180
E/timefortest: 240
让我知道我做错了什么.我的目的是我需要在服务内部启动计数器,并且需要获取该值.
let me know what i did wrong.my purpose is i need to start counter inside service and i need to fetch the value.
推荐答案
我认为您应该删除mod60.这是一个示例.
I think you should remove the mod 60.here is an example.
long timeWhenStopped = SystemClock.elapsedRealtime() - chronometer.getBase();
int seconds = (int) timeWhenStopped / 1000;
这篇关于如何在服务内添加天文钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!