当我回到活动时,更快地运行倒数计时器

时间:2017-07-04 15:32:07

标签: android android-service countdowntimer

我为倒数计时器做了一项服务,在活动中我每隔一秒为显示时间设置一个文本视图:100 - 0,但是当我离开活动并返回时。我看到计时器运行速度非常快,但我希望每秒运行一次。哪里有问题?

MainActivity:

public static final String mBroadcastIntegerAction = "com.example.broadcast.integer";
private IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    showTime = (TextView) findViewById(R.id.textView1); 

    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(mBroadcastIntegerAction);

    Intent serviceIntent = new Intent(this, AppServiceDay.class);
    startService(serviceIntent);
}

@Override
public void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter); 
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(mBroadcastIntegerAction)) {
            int second = intent.getIntExtra("Time", 0);

            showTime.setText("" + second);
        }
    }
};
    @Override
protected void onPause() {
    registerReceiver(mReceiver, mIntentFilter);
//      unregisterReceiver(mReceiver);
    super.onPause();
}

服务:

public class AppServiceDay extends Service {

CountDownTimer cdt;
public static Handler mHandler; 
int downer = 1000;
int time = 100;
int mainTime = 100000;

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    cdt = new CountDownTimer(mainTime, downer) {
        @Override
        public void onTick(long millisUntilFinished) {

            time -= 1;

            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction(MainActivity.mBroadcastIntegerAction);
            broadcastIntent.putExtra("Time", time);
            sendBroadcast(broadcastIntent);
        }

        @Override
        public void onFinish() {
            time = 100;
            this.start();
        }
    };
    cdt.start();

    return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}

1 个答案:

答案 0 :(得分:0)

每次调用 startService 时都会触发 onStartCommand ,而在onStartCommand中,您正在创建新的倒计时对象

在创建新倒计时对象之前添加空检查,它将修复同时运行的重复计时器。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    If(cdt == null) {
        cdt = new CountDownTimer(mainTime, downer) {
            @Override
            public void onTick(long millisUntilFinished) {
                time -= 1;
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction(MainActivity.mBroadcastIntegerAction);

                broadcastIntent.putExtra("Time", time);
                sendBroadcast(broadcastIntent);

            }

            @Override
            public void onFinish() {
                time = 100;
                this.start();
            }
        };
        cdt.start();
    }

    return super.onStartCommand(intent, flags, startId);
}