Android server+BroadcastReceiver倒计时出错

我用server+BroadcastReceiver做了个计时器,现在出现个问题,每次重新进入页面时候倒计时都会累加来减,就是原来是1s来减,第二次进入就是2s来。在 finish中写了取消广播的方法。求组大神
@Override
public void finish() {
super.finish();
if (isreceiver==true) {
Intent service = new Intent(ShareActivity.this, TimeService.class);
service.setAction(TimeService.CLOCK_SERVICE_ACTION);
service.putExtra("method", "stop");
super.sendBroadcast(service);
stopService(service);
unregisterReceiver(clockReceiver);
}
}
private void regReceiver(){
isreceiver = true;
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(CLOCK_ACTION);
super.registerReceiver(clockReceiver, intentFilter);
}
/**
*广播接受者,接受来自ClockService(计时服务)的广播,ClockService每隔一秒
*钟发一次广播
*/
private BroadcastReceiver clockReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
changeTime();//改变TextView中的显示时间
}
};
private void changeTime(){
String stime="";
if(TIME==0){
if (ISOUT==true){
stime="上传截图";
mBtnshare.setEnabled(true);
}else {
stime="分享失败";
mBtnshare.setEnabled(false);
}
super.unregisterReceiver(clockReceiver);
Intent intent=new Intent();
intent.setAction(TimeService.CLOCK_SERVICE_ACTION);
intent.putExtra("method", "stop");
super.sendBroadcast(intent);
}else{
int hour=TIME/(1000*60*60);
int minute=TIME%(1000*60*60)/(60*1000);
int second=(TIME%(1000*60*60))%(60*1000)/1000;
String shour=""+hour,sminute=""+minute,ssecond=""+second;
if(hour<=9){
shour="0"+hour;
}
if(minute<=9){
sminute="0"+minute;
}
if (second<=9){
ssecond="0"+second;
}
if (ISOUT==true) {
stime = shour + ":" + sminute + ":" + ssecond+"后上传截图";
mBtnshare.setEnabled(false);
}else {
mBtnshare.setEnabled(true);
stime = shour + ":" + sminute + ":" + ssecond+"内可再次分享";
}
}
mBtnshare.setText(stime);
}

1个回答

用CountDownTimer

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!