I'm trying to make an alarm app, and I'm really close, but I want the alarm sound to keep playing until the user has manually disabled it via my app (puzzle to turn off alarm type idea). The problem Im having now is that the sound plays up until I pull down the notification drawer, then it stops. Here is the relevant code:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
    notificationBuilder.setContentTitle(alarm.getLabel());
    notificationBuilder.setContentText(alarm.getMessage());
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    notificationBuilder.setSound(alarmSound);
    notificationBuilder.setOngoing(true);
    Notification notification = notificationBuilder.build();
    if(alarm.getVibrate()) {
        notification.defaults |= Notification.DEFAULT_VIBRATE;
    }
    notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_INSISTENT;
    notificationManager.notify(2, notification);

I have tried messing around with the flags, but nothing seems to keep it playing... Thanks for any help in advance.

有帮助吗?

解决方案

Found a solution. Had to use a background Service to play the ringtone rather than the notification manager.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top