首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android通知声音

Android通知声音
EN

Stack Overflow用户
提问于 2013-04-04 19:06:01
回答 16查看 227.8K关注 0票数 163

我已经使用了较新的NotificationCompat构建器,但我无法获得发出声音的通知。它会震动并闪光。android文档中说要设置一个我已经使用过的样式:

代码语言:javascript
复制
builder.setStyle(new NotificationCompat.InboxStyle());

但是没有声音?

完整的代码:

代码语言:javascript
复制
NotificationCompat.Builder builder =  
        new NotificationCompat.Builder(this)  
        .setSmallIcon(R.drawable.ic_launcher)  
        .setContentTitle("Notifications Example")  
        .setContentText("This is a test notification");  


Intent notificationIntent = new Intent(this, MenuScreen.class);  

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
        PendingIntent.FLAG_UPDATE_CURRENT);  

builder.setContentIntent(contentIntent);  
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
// Add as notification  
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
manager.notify(1, builder.build());  
EN

回答 16

Stack Overflow用户

回答已采纳

发布于 2013-04-07 04:06:23

我之前的代码中缺少的内容:

代码语言:javascript
复制
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
票数 267
EN

Stack Overflow用户

发布于 2013-08-01 20:11:34

只需将您的声音文件放在Res\raw\siren.mp3文件夹中,然后使用以下代码:

对于自定义声音:

代码语言:javascript
复制
Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.siren);

对于默认声音:

代码语言:javascript
复制
notification.defaults |= Notification.DEFAULT_SOUND;

对于自定义振动:

代码语言:javascript
复制
long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;

对于默认振动:

代码语言:javascript
复制
notification.defaults |= Notification.DEFAULT_VIBRATE;
票数 164
EN

Stack Overflow用户

发布于 2014-12-05 13:41:46

默认声音的另一种方式

代码语言:javascript
复制
builder.setDefaults(Notification.DEFAULT_SOUND);
票数 54
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15809399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档