如何通过单击通知取消警报

时间:2019-03-29 12:42:41

标签: android

如何在单击通知时取消警报/停止响铃并同时关闭通知?我这里有两个类,即MyBroadcastReceiver和AddVitamin类,在其中设置警报。

这是针对AddVitamin类的。我将不再发布整个代码。 startAlarm()方法用于启动警报...

@Override
public void onTimeSet(TimePicker view, int hoursOfDay, int minute){

    c.set(Calendar.HOUR_OF_DAY, hoursOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);

}

private void updateTimeText(Calendar c){
    String timeText = " ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    time.setText(timeText);
}

private void startAlarm(Calendar c){
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, FLAG_UPDATE_CURRENT);
    if(c.before(Calendar.getInstance())){
        c.add(Calendar.DATE, 1);
    }

  if (!aSwitch.isChecked()){
          alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
          switchu = true;
  }
    else{
        switchu = false;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS), pendingIntent);
  }

}

然后我有MyBroadcastReceiver类

public class MyBroadcastReceiver extends BroadcastReceiver

{
Uri notif;
Ringtone r;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent){
     notif = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
     r = RingtoneManager.getRingtone(context,notif);
    String CHANNEL_ID = "Caireen";// The id of the channel.
    CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);


    Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(0b11111010000);


    Notification notification = new Notification.Builder(context)
            .setContentTitle("It's time to take baby's vitamin!")
            .setContentText("Click to stop alarm.")
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launchercaireen_round).build();

    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification.flags|= Notification.FLAG_AUTO_CANCEL;
    manager.notify(0,notification);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        manager.createNotificationChannel(mChannel);
    }
    r.play();
}

应该在单击通知时将其关闭。我尝试了setAutoCancel(true),但是它不起作用。另外,单击通知时,我不会如何自行停止警报。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用此方法取消警报

  private PendingIntent notifyIntent;

  public void unsetAlarm(View v) {
    Intent myIntent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
    notifyIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, myIntent, 0); 

    alarmManager.cancel(notifyIntent);
  }