2010-07-21 109 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