2010-07-21 108 views
17

我已经创建,创建通知,使用下面的代码的应用程序:如何使用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)

+0

您是否拥有振动许可? – Sephy 2010-07-21 10:06:53

+0

我在Nexus One上测试了您的代码,并获得了许可,我确实得到了振动但不是领导...仍在挖掘 – Sephy 2010-07-21 10:28:39

+0

同样的事情,我错过了振动许可,现在这部分工作正常。 – SirDarius 2010-07-21 10:36:27

回答

28

添加权限清单文件

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

编辑

灯来尝试添加他们明确,默认的光可以被配置成nolight

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

我也建议,但经过一番思考后,那不可能。当你没有权限时,应用程序崩溃不是吗?所以他不会看到其他工作 – Sephy 2010-07-21 10:23:04

+0

这就是我认为,一个失踪的权限应该抛出一个异常。 – SirDarius 2010-07-21 10:31:21

+0

来完成我以前的评论,添加缺少的权限做了窍门,但仍然没有灯光,我没有找到具体的许可为此目的 – SirDarius 2010-07-21 10:38:27

14

除了Pentium10的回答是:

让您的设备进入睡眠状态,灯光会持续亮起! ;)

+0

Hello OneWorld尝试上面的代码,并把这个代码也note.defaults | = Notification.DEFAULT_SOUND; note.defaults | = Notification.DEFAULT_VIBRATE;通过这种振动和声音的作品,但即使我把我的设备放在睡眠中,光线也不起作用,光线会熄灭。 – 2011-10-17 09:26:30

+1

对于任何未来遇到此问题的人,请确保手机处于关闭状态,正如OneWorld所说。还要确保通知盘已经没有被拉下(即用户没有看到通知)。在这种情况下LED只会闪烁。 – ashishduh 2014-02-06 19:12:35