如何使用 Android 通知 api 启用振动和灯光?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3297835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I enable vibration and lights using the Android notifications api?
提问by SirDarius
I have created an application that creates notifications, using the following code:
我使用以下代码创建了一个创建通知的应用程序:
// 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);
The notification works, and the correct ringtone is used.
通知有效,并且使用了正确的铃声。
However, even though the preferences are correctly activated and notification flags are correctly set (I checked by debugging), the notification never vibrates and never cause the lights to be activated.
但是,即使正确激活了首选项并且正确设置了通知标志(我通过调试检查过),通知也不会振动,也不会导致灯被激活。
I would have blamed my phone's settings, but every other app using notifications, like messaging, gmail, and others correctly use all these features.
我会责怪我手机的设置,但其他所有使用通知的应用程序,如消息、gmail 和其他应用程序都正确地使用了所有这些功能。
May someone know what I did wrong ? (my phone is a HTC Hero with Android 2.1)
有人知道我做错了什么吗?(我的手机是安卓 2.1 的 HTC Hero)
回答by Pentium10
Add permission to your manifest file
为清单文件添加权限
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
EDIT
编辑
For Lights try adding them explicitly, the Default light might be configured to be nolight
对于灯光尝试明确添加它们,默认灯光可能被配置为无光
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
回答by OneWorld
In addition to Pentium10'S answer:
除了Pentium10的回答:
Put your device to sleep and the lights will go on! ;)
让您的设备进入睡眠状态,灯就会亮起来!;)