使用cancel()后,通知已关闭并重新打开;

时间:2018-09-05 13:06:20

标签: java android countdown

我的应用程序有问题。我有2个按钮“开始”和“停止”,当我单击“开始”时,在倒数计时器的每个滴答声中都会创建和更新通知,但是我想在按下“停止”按钮时关闭通知。我使用了notificationmanager.cancel(id),但通知已关闭,然后再次重新打开。我做错了什么这是我的代码:

private void startButtonClick() {
    try {
        time = Integer.parseInt(timeInMinutes.getText().toString());
        if (time == 0) {
            throw new NumberFormatException("Time can't be lower than 1");
        }
        startButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.VISIBLE);
        timeInMinutes.setVisibility(View.INVISIBLE);
        countDownTextView.setVisibility(View.VISIBLE);
        final NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Timer")
                        .setContentText("12312312");
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        new CountDownTimer(time * 60000, 500) {
            public void onTick(long millisUntilFinished) {
                if (isCanceled) {
                    cancel();
                    buttonsVisibility();
                    isCanceled = false;
                }
                int minutes = (int) (millisUntilFinished / 1000) / 60;
                int seconds = (int) (millisUntilFinished / 1000) % 60;
                countDownTextView.setText("Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                mBuilder.setContentText("Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                notificationManager.notify(3, mBuilder.build());
            }

            public void onFinish() {
                turnOffBluetooth();
                turnOffWiFi();
                turnOffAudio();
                turnOnSilentMode();
                buttonsVisibility();
                notificationManager.cancel(3);
            }
        }.start();
    }
    catch (NumberFormatException e){
        Toast.makeText(getApplicationContext(), "Enter time. Only numbers!", Toast.LENGTH_SHORT).show();
    }
}
private void pauseButtonClick() {
    Toast.makeText(getApplicationContext(), "Timer off", Toast.LENGTH_LONG).show();
    startButton.setVisibility(View.VISIBLE);
    pauseButton.setVisibility(View.INVISIBLE);
    isCanceled = true;
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(3);
}

2 个答案:

答案 0 :(得分:0)

像这样修改onTick()方法:

file.cc

答案 1 :(得分:0)

即使您取消了计时器,但尚未完成该方法,因此它将继续执行以避免您可以尝试使用此方法

if (isCanceled) {
                    cancel();
                    isCanceled = false;
                } else {
                    int minutes = (int) (millisUntilFinished / 1000) / 60;
                    int seconds = (int) (millisUntilFinished / 1000) % 60;
                    mBuilder.setContentText(
                        "Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                    notificationManager.notify(3, mBuilder.build());
                }