문제

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