如何在通知点击时停止闹钟铃声

时间:2020-04-23 10:03:47

标签: android android-notifications alarm

我想使用ringtone.stop()方法在Notification中执行.addAction()功能

@Override
public void onReceive(Context context, Intent intent) {

    final Alarm alarm = intent.getBundleExtra(BUNDLE_EXTRA).getParcelable(ALARM_KEY);
    if(alarm == null) {
        Log.e(TAG, "Alarm is null", new NullPointerException());
        return;
    }

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


    final int id = alarm.notificationId();


    alarmsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    ringtone = RingtoneManager.getRingtone(context, alarmsound);
    //Alarm Sound Play
    ringtone.play();

    final NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    createNotificationChannel(context);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_alarm_white_24dp);
    builder.setColor(ContextCompat.getColor(context, R.color.accent));
    builder.setContentTitle(context.getString(R.string.app_name));
    builder.setContentText(alarm.getLabel());
    builder.setTicker(alarm.getLabel());
    builder.setVibrate(new long[] {1000,500,1000,500,1000,500});
    builder.setSound(alarmsound);
    builder.setContentIntent(launchAlarmLandingPage(context, alarm));
    builder.setAutoCancel(true);
    builder.setPriority(Notification.PRIORITY_HIGH);
    PendingIntent onDestroy = onCancel();
    builder.addAction(R.drawable.ic_repeat_black_24dp,  onDestroy);

    manager.notify(id, builder.build());

    //Reset Alarm manually
    setReminderAlarm(context, alarm);
}
public static PendingIntent onCancel(){
        ringtone.stop();
        return null;
    }

0 个答案:

没有答案