一起停止警报和警报铃声

时间:2017-04-06 17:42:02

标签: java android notifications broadcastreceiver alarm

我正在开发一个警报应用程序。 当它发出警报时,它会通过禁用按钮发出通知。 因此,当我点击禁用按钮时,它应该停止警报铃声和警报。

所以当我点击禁用按钮时,它会调用以下方法。 它会停止闹钟。 但我想知道这个铃声是否会在下一个日期同时活着,或者它会消除警报好吗?

public void dismissRingtone() {
    // stop the alarm rigntone
    Intent i = new Intent(this, RingtonePlayingService.class);
    stopService(i);

    // also dismiss the alarm to ring again or trigger again
    AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    aManager.cancel(pendingIntent);

    // Canceling the current notification
    NotificationManager notificationManager =
            (NotificationManager)getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    notificationManager.cancel(321);
}

1 个答案:

答案 0 :(得分:0)

尝试,它将取消未来的警报。

    Intent intentAlarm = new Intent(this, AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Set the alarm for a particular time.

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    try {
        pendingIntent.cancel();
        alarmManager.cancel(pendingIntent);
    } catch (Exception e) {
        e.printStackTrace();

    }