如何使用 Android 通知 api 启用振动和灯光? [英] How can I enable vibration and lights using the Android notifications api?

查看:27
本文介绍了如何使用 Android 通知 api 启用振动和灯光?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我会责怪我手机的设置,但其他所有使用通知的应用程序(如消息、gmail 和其他应用程序)都正确地使用了所有这些功能.

I would have blamed my phone's settings, but every other app using notifications, like messaging, gmail, and others correctly use all these features.

有人知道我做错了什么吗?(我的手机是安卓 2.1 的 HTC Hero)

May someone know what I did wrong ? (my phone is a HTC Hero with Android 2.1)

推荐答案

向清单文件添加权限

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

编辑

对于灯光尝试明确添加它们,默认灯光可能被配置为无光

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;

这篇关于如何使用 Android 通知 api 启用振动和灯光?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆