簡體   English   中英

如何使用Android通知api啟用振動和燈光?

[英]How can I enable vibration and lights using the Android notifications 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和其他正確使用所有這些功能。

願有人知道我做錯了嗎? (我的手機是搭載Android 2.1的HTC Hero)

添加清單文件的權限

<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;

除了奔騰10'的答案:

讓你的設備睡覺,燈會繼續! ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM