Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通知通道-是否可以在它之后更改LightColor';设定好了吗?_Java_Android_Notifications_Channel_Led - Fatal编程技术网

Java 通知通道-是否可以在它之后更改LightColor';设定好了吗?

Java 通知通道-是否可以在它之后更改LightColor';设定好了吗?,java,android,notifications,channel,led,Java,Android,Notifications,Channel,Led,我正在尝试使用JavaScript界面返回的颜色更改setLightColor。不幸的是,NotificationCompat.Builder(context,CHANNEL\u ID).setLights对API>=26绝对没有影响,所以我不能使用Intent.putExtra或任何类似的东西 在它已经设置好之后,甚至可以更改它吗?我希望它是动态的 编辑似乎对我想要什么有一些误解。我不想触摸广播接收器。它很好用。我想更改通知频道。它没有更新setLightColor(Color.\uuuu)

我正在尝试使用JavaScript界面返回的颜色更改
setLightColor
。不幸的是,
NotificationCompat.Builder(context,CHANNEL\u ID).setLights对API>=26绝对没有影响,所以我不能使用
Intent.putExtra
或任何类似的东西

在它已经设置好之后,甚至可以更改它吗?我希望它是动态的

编辑似乎对我想要什么有一些误解。我不想触摸
广播接收器
。它很好用。我想更改通知频道。它没有更新
setLightColor(Color.\uuuu)

protectedvoid onCreate中

String jobColor = someColor; // Will be filled in by other code - different colour every time
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Channel_Name";
    String description = "Channel_Description";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    channel.enableLights(true);
    channel.setLightColor(Color.parseColor(jobColor)); // Dynamically set from above
    channel.enableVibration(true);
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}
我的BroadcastReceiver-setLight不适用于API 26或更高版本,我相信

public class AlarmReceiver extends BroadcastReceiver {
private final String CHANNEL_ID = "some_channel";
@Override
public void onReceive(Context context, Intent intent) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , new Intent(context, MainPage.class), 0);
    String jobColor = intent.getStringExtra("jobColor");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle("Upcoming Shift!")
            .setContentText("Shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime"))
            .setStyle(new NotificationCompat.BigTextStyle().bigText("You have a shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime")))
            .setLights(Color.parseColor(jobColor), 10000, 1000)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(12345, mBuilder.build());
}
}

从createNotificationChannel()说明:

这也可用于恢复已删除的频道和更新现有频道的 *名称、描述、组和/或重要性

所以你可以试试这个:

  NotificationManager notificationManager = getSystemService(NotificationManager.class);
            //find created channel
            NotificationChannel channel = notificationManager.getNotificationChannel("id");
            if (channel != null){
                //set new color
                channel.setLightColor(0);
                //update channel
                notificationManager.createNotificationChannel(channel);
            }

如果不删除频道,则无法通过编程方式更改频道颜色通知、重要性等

因为用户可能已手动更改灯光颜色

要以编程方式实现此目的,请获取频道并使用新id创建新频道。删除旧频道如果使用以前的id创建频道,您的更改将不会反映出来

为了便于参考,请检查WhatsApp应用程序尝试更改应用程序的铃声,并在左下角的频道中查看“频道已删除”消息


Source

每次要更新当前中的内容时,都会更新通知。这会引发错误,因为getNotificationChannel在第一次运行时将变为null。我只是把
getNotificationChannel(CHANNEL\u ID)
放在if中,它就运行了。然而,这无论如何都不起作用。if中有一个sys.out,因此我可以判断
if
是否运行,它确实运行了。但是颜色的改变不适用。是的,就是这样。我想可能还有别的办法。谢谢