我已经创建,创建通知,使用下面的代码的应用程序:如何使用Android通知API启用振动和灯光?
// 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等)都可以正确使用所有这些功能。
可能有人知道我做错了什么吗? (我的手机是HTC Hero采用了Android 2.1)
您是否拥有振动许可? – Sephy 2010-07-21 10:06:53
我在Nexus One上测试了您的代码,并获得了许可,我确实得到了振动但不是领导...仍在挖掘 – Sephy 2010-07-21 10:28:39
同样的事情,我错过了振动许可,现在这部分工作正常。 – SirDarius 2010-07-21 10:36:27