【发布时间】:2012-12-25 15:16:29
【问题描述】:
我使用以下代码创建了一个创建通知的应用程序:
// notification
Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// parameters
String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
if (ringtone.length() > 0) {
notification.sound = Uri.parse(ringtone);
notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
}
boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
if (useVibrator) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
if (useLed) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
// alert
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, text);
notification.contentView = contentView;
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(1, notification);
通知有效,并且使用了正确的铃声。
但是,即使正确激活了首选项并且正确设置了通知标志(我通过调试进行了检查),通知也不会振动,也不会导致灯被激活。
我会责怪我手机的设置,但其他所有使用通知的应用程序,如消息、gmail 和其他应用程序都正确使用了所有这些功能。
有人知道我做错了什么吗? (我的手机是安卓2.1的HTC Hero)
【问题讨论】:
-
你有震动权限吗?
-
我在我的 Nexus One 上测试了你的代码,得到了许可,我确实得到了振动,但没有得到 LED...仍在挖掘
-
同样的事情,我错过了振动许可,现在该部分正在工作。
-
@SirDarius :即使对我来说,灯也不起作用。你找到解决方案了吗?
-
好吧,正如我在评论中所说,它确实在从接受的答案设置权限后开始工作。
标签: android notifications