How to stop android notification ringtone after 30

2019-03-15 21:09发布

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

3条回答
我只想做你的唯一
2楼-- · 2019-03-15 21:58

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);
查看更多
Luminary・发光体
3楼-- · 2019-03-15 22:07

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.

查看更多
We Are One
4楼-- · 2019-03-15 22:12

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).

查看更多
登录 后发表回答