我在Android 8下有一部手机,我开始使用Android Studio进行Android编程。一些应用程序,如Snapchat或Facebook,当通知出现时,我的手机灯会以自定义颜色(黄色和蓝色)为主。
我想对我的应用做同样的事情,我搜索了很多但没有任何作用,通知出现但不是白光。我检查了手机设置,我的应用程序可以点亮LED。
public class Notification
{
public Notification(String channelId, String channelName, Context context, String title, String body, Intent intent)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
NotificationChannel mChannel = new
NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder nbuilder = new NotificationCompat.Builder(context, channelId)
.setLights(Color.WHITE, 500, 100) // doesn't work
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) // doesn't work
.setContentTitle(title)
.setContentText(body);
TaskStackBuilder stackBuilder =
TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
nbuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, nbuilder.build());
}
}
我尝试在setLights()
之后调用setVibrate()
和setContentText()
,我尝试在notify()之前调用这些空格,但它没有改变任何内容。
我在onCreate()
:
new Notification("channel-01", "MathWellan Notification", this, "Twitter", "Follow me on Twitter !", new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=MathWellan")));
抱歉我的英语不好,我是法国人,我希望你能帮助我! 谢谢提前:)
答案 0 :(得分:1)
使用以下代码行更改通知LED颜色:your_notification.ledARGB = Color.YOUR_COLOR;
用法示例:
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notif.cancel(1); // clear previous notification
final Notification notification = new Notification();
notification.ledARGB = Color.MAGENTA;
notification.ledOnMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.notify(1, notification);
注意:测试时屏幕应该被锁定,因为通知LED仅在屏幕关闭时才会突出显示。 也许这是你的问题。