问题描述
- 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);
}
时间: 2024-09-24 01:57:30