如何使用Android通知api启用振动和灯光?

时间:2010-07-21 09:24:55

标签: android notifications

我使用以下代码创建了一个创建通知的应用程序:

// 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和其他正确使用所有这些功能。

有人知道我做错了吗? (我的手机是搭载Android 2.1的HTC Hero)

2 个答案:

答案 0 :(得分:28)

添加清单文件的权限

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

修改

对于Lights尝试显式添加它们,默认灯可能配置为nolight

notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;

答案 1 :(得分:14)

除了Pentium10的回答:

让您的设备进入睡眠状态,指示灯会亮起! ;)