How to stop android notification ringtone after 30 seconds?
Asked Answered
J

3

10

I'm writing an app that notifies a user of very time sensitive information, and I think a lot of them would want to use a phone ringtone instead of just the short notification sounds. I enabled this feature by setting android:ringtoneType="all" in my PreferenceScreen, and it works great EXCEPT that when a phone ringtone is chosen, it keeps playing FOREVER until the user touches the notification bar... how can I get it to turn off after 30 seconds or so, without canceling the notification?

heres the code I'm using in my C2DM receiver:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(....)
....
String ringtone = PreferenceManager.getDefaultSharedPreferences(context).getString("ringtone", "");
notif.sound = Uri.parse(ringtone);
notif.audioStreamType = AudioManager.STREAM_NOTIFICATION;
nm.notify(1, notif);
Jettiejettison answered 14/7, 2012 at 19:17 Comment(1)
offtopic: C2DM is deprecated rewrite it using Google Clound Messaging (GCM)Christiano
H
12

This should do it.

....
mediaPlayer.start();
....
final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mediaPlayer.isPlaying())
                mediaPlayer.stop();
        }
    }, timeout);
Hexapla answered 21/7, 2012 at 17:36 Comment(1)
Where you got the mediaPlayer instance from?Gredel
P
7

Android does not give you access to interfere with the notification playing the sound but if you have the full control of the ringtone you are playing, then you can stop it after 30 seconds or do whatever you want with it.

Here, I am assuming that you are using a background service to execute the notification.

As a preliminary step, you should make the notification without sound. Just a default, simple taskbar notification. Then, we will use our own MediaPlayer object to play the ringtone for 30 seconds.

1) IF YOU ARE EXECUTING THE NOTIFICATION ON THE SPOT: Create a MediaPlayer object, start playing the ringtone the user picked right after you start the notification. Then count 30 seconds with a Timer and TimerTask (see example here on how to use them) or ScheduledThreadPoolExecutor and call MediaPlayer stop() to stop the ringtone after 30 seconds.

2) IF YOU ARE SCHEDULING THE NOTIFICATION FOR A LATER TIME: As soon as you set your notification, create an AlarmManager and schedule it so that it will be triggered at the same time with your notification. Your alarm could trigger another background service that you wrote in which you play the ringtone for 30 seconds using a MediaPlayer object as explained in part 1).

In both cases, the notification will still be there and the way you will play the ringtone will totally be independent of it. You will have full control over the ringtone. You can even replay it for another 30 seconds after some scheduled time if the user has not checked the notification till that time (since you are saying that the notification delivers a very time sensitive information, otherwise I wouldn't bug the user with ringtones playing insistingly).

Prelude answered 21/7, 2012 at 1:44 Comment(4)
is there a way when the user touches the notification bar for me to detect that and turn off the alarm? Other than it would be pretty annoying to have the ringtone keep playing.. If thats the only alternative i might just advise them to download some extended notifications tones or somethingJettiejettison
Of course, there are many ways to detect it and it all depends on what you want your notification to do. Then, you should communicate between the resulting activity receiving your notification's intent and your service holding the mediaplayer object and cancel playback. Here is a detailed tutorial on how to capture notifications: developer.android.com/guide/topics/ui/notifiers/…Prelude
I'm not talking about when the notification is clicked, or even dismissed. I mean normally if a user just the touches the top of the notification bar, no more sound is played. I dont see a way to capture that, am I wrong?Jettiejettison
Have a look at this(developer.android.com/reference/android/accessibilityservice/…) and this(developer.android.com/guide/topics/ui/accessibility/…). Those might be the only way to figure it out. Other than that, there isn't a way that I know of.Prelude
G
1

An simple workaround would be to cancel the notification after 30 secs and create a new one.

You could even clone the original Notification and just remove the ring-tone for the new one.

Gredel answered 26/7, 2012 at 10:53 Comment(1)
how do I detect if the notification has already been cancelled by the user, so I'm not just putting up a duplicate notification after they've already clicked it?Jettiejettison

© 2022 - 2024 — McMap. All rights reserved.