android如何取消或替换通知

时间:2018-12-16 17:02:30

标签: java android android-studio android-notifications android-broadcastreceiver

我有android进度栏通知,但我不知道如何取消它。 通知需要多少秒才能成为EDITTEXT的进度条 当我按下按钮2次时,第一个按钮不会取消,新的按钮从第一个按钮开始,并且进度栏会一起工作。 那么我如何取消第一个并开始新的呢? 还有一个问题 是否有可能从Broadcastreceiver调用进度栏通知?

这是代码

mButtonStartPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int time = Integer.parseInt(mEditTextInput.getText().toString());
                Intent intent = new Intent(MainActivity.this, Alarm1.class);
                PendingIntent p1 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
                AlarmManager a = (AlarmManager) getSystemService(ALARM_SERVICE);
                a.set(AlarmManager.RTC, System.currentTimeMillis() + time * 1000, p1);

                if (mTimerRunning) {
                    pauseTimer();
                }else {
                    startTimer();
                }
                String value= mEditTextInput.getText().toString();
                final int input2=Integer.parseInt(value);
                Intent resultInent = new Intent(Avstangingg.this, Avstangingg.class);
                PendingIntent resultPendingIntent = PendingIntent.getActivity(Avstangingg.this, 1, resultInent, PendingIntent.FLAG_UPDATE_CURRENT);

                final NotificationCompat.Builder notification = new NotificationCompat.Builder(Avstangingg.this, CHANNEL_2_ID)
                        .setSmallIcon(R.drawable.index)
                        .setContentTitle("time")
                        .setContentText("progress")
                        .setPriority(NotificationCompat.PRIORITY_LOW)
                        .setOngoing(true)
                        .setAutoCancel(true)
                        .setOnlyAlertOnce(true)
                        .setContentIntent(resultPendingIntent)
                        .setProgress(input2, 0, false);
                notificationManager.notify(2, notification.build());

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(0);
                        for (int progress = 0; progress <= input2; progress += 1) {
                            notification.setProgress(input2, progress, false);
                            notificationManager.notify(2, notification.build());
                            SystemClock.sleep(1000);
                        }
                        notification.setContentText("end")
                                .setProgress(0, 0, false)
                                .setOngoing(false);
                        notificationManager.notify(2, notification.build());

                    }
                }).start();

这是我的BroadcastReceiver

public class Alarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Finish", Toast.LENGTH_LONG).show();
        Vibrator v = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        v.vibrate(7000);

        MediaPlayer mp;
        mp = MediaPlayer.create(context, R.raw.alert);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.reset();
                mp.release();
                mp = null;
            }

        });
        mp.start();
    }
}

0 个答案:

没有答案