2011-12-07 214 views
7

我的活动中有一个倒数计时器,当它达到零时,我会向状态栏发送通知,该通知还会播放自定义wav文件以提醒用户。我只想要状态栏通知出现,如果用户当时没有使用应用程序,但我仍然希望播放wav文件,无论定时器应用程序是否可见。只有播放声音的Android通知

是否可以让Android通知只播放音频提示?我试过使用MediaPlayer来播放wav文件,但是它使用自己的音量设置,而不是常规的通知音量,所以如果您的媒体音量很低,通知声音也会以低音量播放,这也是我不想要的。我希望声音以与其他通知相同的音量播放。

我使用这个: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

和供应的声音是这样的:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
+0

是的,它可以只播放一段音频警报。 –

+1

谢谢@ coder_For_Life22,你会告诉我如何? – Daniel

回答

5

在 '添加声音' 在http://developer.android.com/guide/topics/ui/notifiers/notifications.html 节有这样一个字条:

Note: If the defaults field includes DEFAULT_SOUND, then the default sound overrides any sound defined by the sound field.

所以,如果你只想定制声音,你可以使用 -

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE; 
3

根据我的实验,确实可以发送通知,但不会将其显示在通知区域中,但仍会播放声音。

您只需构建通知构建器并为其设置声音,但跳过待处理的意图,图标,内容标题和内容文本即可。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setSound(your_sound_uri); 
notificationManager.notify(1234, builder.build()); 
+1

这些答案特别有用,因为文档对此有错误。 http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification说,“通知对象*必须包含以下内容:一个小图标,由setSmallIcon()设置;一个标题,一组通过setContentTitle();详细文本,由setContentText()设置“ – Jerry101

+2

该图标是必需的。这会导致错误:java.lang.IllegalArgumentException:无效的通知(无效的小图标) –

0
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

Notification mNotification = new Notification.Builder(this) 

      .setContentTitle("New Post!") 
      .setContentText("Here's an awesome update for you!") 
      .setSmallIcon(R.drawable.ic_lancher) 
      .setContentIntent(pIntent) 
      .setSound(soundUri) 
      .build(); 
0
NotificationCompat.Builder builder= new NotificationCompat.Builder(this); 

三种方式setSound

builder.setDefaults(Notification.DEFAULT_SOUND); 
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);