如何在30秒后停止Android的通知铃声? [英] How to stop android notification ringtone after 30 seconds?

查看:727
本文介绍了如何在30秒后停止Android的通知铃声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,通知时间非常敏感信息的用户,我想很多人会想用手机铃声,而不仅仅是短期的通知声音。我启用此功能通过设置安卓ringtoneType =所有在我的preferenceScreen,和它的伟大工程不同的是,手机铃声在选择时,它不断打FOREVER直到用户触摸通知栏......我怎样才能得到它30秒打完关闭,没有取消的通知?

继承人的code,我用我的C2DM接收器:

  NotificationManager纳米=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
注意notif =新的通知(....)
....
串铃声= preferenceManager.getDefaultShared preferences(上下文).getString(铃声,);
notif.sound = Uri.parse(铃声);
notif.audioStreamType = AudioManager.STREAM_NOTIFICATION;
nm.notify(1,notif);
 

解决方案

这应该这样做。

  ....
mediaPlayer.start();
....
最终的处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable(){
        @覆盖
        公共无效的run(){
            如果(mediaPlayer.isPlaying())
                mediaPlayer.stop();
        }
    }, 时间到);
 

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

解决方案

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

这篇关于如何在30秒后停止Android的通知铃声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆