【问题标题】:Stopping alarm and alarm ringtone together一起停止闹钟和闹钟铃声
【发布时间】:2017-04-06 17:42:02
【问题描述】:

我正在开发一个警报应用程序。 当它触发警报时,它会抛出带有禁用按钮的通知。 所以当我点击禁用按钮时,它应该会停止闹钟铃声和闹钟。

所以当我单击禁用按钮时,它会调用以下方法。 它停止警报。 但是我想知道这个铃声是否会在下一个日期同时存在,或者它会永久删除警报?

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);
}

【问题讨论】:

    标签: java android notifications broadcastreceiver alarm


    【解决方案1】:

    试一试,它将取消以后的警报。

        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();
    
        }
    

    【讨论】:

    • 但是在我的代码中我取​​消了当前的警报。但是第二天的警报还会存在吗?我想知道
    • 广播接收器 + 已安排的待处理意图将被取消,除非您不再安排。
    • 所以如果我使用你的代码,它将取消两者。我是 r8.. 谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多