我已经使用了较新的NotificationCompat构建器,但我无法获得发出声音的通知。它会震动并闪光。android文档中说要设置一个我已经使用过的样式:
builder.setStyle(new NotificationCompat.InboxStyle());
但是没有声音?
完整的代码:
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());
发布于 2013-04-07 04:06:23
我之前的代码中缺少的内容:
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
发布于 2013-08-01 20:11:34
只需将您的声音文件放在Res\raw\siren.mp3
文件夹中,然后使用以下代码:
对于自定义声音:
Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.siren);
对于默认声音:
notification.defaults |= Notification.DEFAULT_SOUND;
对于自定义振动:
long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
对于默认振动:
notification.defaults |= Notification.DEFAULT_VIBRATE;
发布于 2014-12-05 13:41:46
默认声音的另一种方式
builder.setDefaults(Notification.DEFAULT_SOUND);
https://stackoverflow.com/questions/15809399
复制相似问题