如何在30秒后停止Android的通知铃声?通知、铃声、如何在、Android

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

我正在写一个应用程序,通知时间非常敏感信息的用户,我想很多人会想用手机铃声,而不仅仅是短期的通知声音。我启用此功能通过设置安卓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();
        }
    }, 时间到);
 
微软 你的手机 应用更新 支持Android通知同步

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

阅读全文

相关推荐

最新文章