更改通知的 LED 颜色

发布于 2024-11-10 02:10:16 字数 1779 浏览 0 评论 0原文

我基本上只是在尝试 Android 开发,几天前我遇到了这个名为“Go SMS Pro",除其他外,它可以设置不同颜色的通知(蓝色、绿色、橙色、粉色和浅蓝色)。因此,我尝试在自己的应用程序中自己执行此操作,但是我无法更改 LED 的颜色和闪烁内部。我目前使用这段代码:

public class MainActivity extends Activity {
  static final int NOTIFICATION_ID = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(buttonOnClick);
  }

  public OnClickListener buttonOnClick = new OnClickListener() {

    @Override
    public void onClick(View v) {
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

      Notification notification = new Notification(R.drawable.icon, "Hello", System.currentTimeMillis());
      notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
      notification.ledARGB = Color.BLUE;
      notification.ledOnMS = 1000;
      notification.ledOffMS = 300;

      Context context = getApplicationContext();
      CharSequence contentTitle = "My notification";
      CharSequence contentText = "Hello World!";
      Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

      mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
  };
}

但正如我所说,它并没有按照我想要的方式工作;相反,它只是以默认延迟以常规绿色闪烁,而不是我在代码中设置的延迟。

任何人都可以看到我的代码有什么问题,或者知道我是否需要做其他事情来实现这一目标?

I am basically just experimenting with Android development, and a couple of days ago I came across this app called "Go SMS Pro", which, among other things, can set up notifications in different colors (blue, green, orange, pink and light blue). So, I have tried to do this myself in my own app, however I cannot change neiher the color nor the blinking internal of the LED. I currently use this code:

public class MainActivity extends Activity {
  static final int NOTIFICATION_ID = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(buttonOnClick);
  }

  public OnClickListener buttonOnClick = new OnClickListener() {

    @Override
    public void onClick(View v) {
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

      Notification notification = new Notification(R.drawable.icon, "Hello", System.currentTimeMillis());
      notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
      notification.ledARGB = Color.BLUE;
      notification.ledOnMS = 1000;
      notification.ledOffMS = 300;

      Context context = getApplicationContext();
      CharSequence contentTitle = "My notification";
      CharSequence contentText = "Hello World!";
      Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

      mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
  };
}

But as I said, it doesn't work the way I want it to; instead it just blinks in regular green with the default delay, and not the one I have set in my code.

Can anyone see what is wrong with my code, or know if I have to do something else to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

三五鸿雁 2024-11-17 02:10:16

您可以使用以下代码:

private static final int LED_NOTIFICATION_ID= 0; //arbitrary constant

private void RedFlashLight() {
    NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
    Notification notif = new Notification();
    notif.ledARGB = 0xFFff0000;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100; 
    notif.ledOffMS = 100; 
    nm.notify(LED_NOTIFICATION_ID, notif);
}

您可以使用 android.graphics.Color 类中的 int 属性来获取颜色,而不是像示例所示使用 ARGB 值(例如 Color.WHITE)

You can use this code:

private static final int LED_NOTIFICATION_ID= 0; //arbitrary constant

private void RedFlashLight() {
    NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
    Notification notif = new Notification();
    notif.ledARGB = 0xFFff0000;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100; 
    notif.ledOffMS = 100; 
    nm.notify(LED_NOTIFICATION_ID, notif);
}

Instead of using ARGB value as the example show, you can use int property inside android.graphics.Color class to get the color as well (e.g. Color.WHITE)

终遇你 2024-11-17 02:10:16

LED 灯是 Android 手机中的一个非常非标准的功能。如果您依赖它们,您将错过很大一部分用户群(例如,考虑一下 SGS 手机,它甚至没有 LED)。

也就是说,id int 字段 ledARGB 没有用,您可能需要查看该 APK 中的一些 JNI 调用。我的猜测是,根据运行的设备,它将有不同的方法。

Leds are a quite non-standard feature in android phones. If you depend in them, you will miss a good chunk of the user base (consider, for example, the SGS phones, which do not even have leds).

That said, id the int field ledARGB was not useful, you might need to look into some JNI call from that APK. My guess is that it will have different methods depending on the device in which is running.

戏蝶舞 2024-11-17 02:10:16

您是否尝试过: .setLights(Color.BLUE, 500, 500) ?

在 S3、N5、N4、Nexus 上也能正常工作。

Did you try: .setLights(Color.BLUE, 500, 500) ?

Works fine on S3, N5, N4, Nexus one too..

红尘作伴 2024-11-17 02:10:16

FLAG_SHOW_LIGHTSNotification.Builder.setLights(int,int,int); 自 Android O(API 级别 26)以来已弃用如果您计划在 API 级别使用此功能26 或更多请查看 NotificationChannel

<一个href="https://developer.android.com/preview/features/notification-channels.html#CreatingChannels" rel="noreferrer">示例:

NotificationChannel mChannel = new NotificationChannel(id, name, importance);
.....
.....
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);

但在这个新实现中,您可能无法控制 LED 毫秒数 & LED 关闭毫秒将依赖于硬件。

FLAG_SHOW_LIGHTS and Notification.Builder.setLights(int,int,int); are deprecated since Android O ( API level 26 ) If you plan to use this feature in API level 26 or greater have look at NotificationChannel

Example :

NotificationChannel mChannel = new NotificationChannel(id, name, importance);
.....
.....
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);

But in this new implementation You may not have control over LED on milli-seconds & LED off milli-seconds it will be dependent on hardware.

简美 2024-11-17 02:10:16

尝试使用十六进制颜色,包括 alpha 值并将默认值设置为 0:

notification.defaults = 0;
notification.ledARGB = 0xff0000ff;

另外,通知界面是这样说的:

public int ledARGB
Since: API Level 1

The color of the led. The hardware will do its best approximation.

我假设您的硬件具有所有颜色,但可能没有。

Try using the hex color, include an alpha value and set the defaults to 0:

notification.defaults = 0;
notification.ledARGB = 0xff0000ff;

Also, the notification interface says this:

public int ledARGB
Since: API Level 1

The color of the led. The hardware will do its best approximation.

I'm assuming your hardware has all the colors, but it may not.

空‖城人不在 2024-11-17 02:10:16

对 LED 颜色的支持确实参差不齐。尝试拔下 USB 电缆并确保没有其他应用程序同时尝试修改 LED。同时关闭屏幕。

Support for the LED colors is really spotty. Try unplugging your USB cable and making sure that no other app is trying to modify the LED at the same time. Also turn off the screen.

傲性难收 2024-11-17 02:10:16

看看下面的来源。

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setSmallIcon(getNotificationIcon())
                        .setColor(0xff493C7C)
                        .setContentTitle(ctx.getString(R.string.app_name))
                        .setContentText(msg)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setLights(0xff493C7C, 1000, 1000)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(styledMsg));

Have a look on source below.

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setSmallIcon(getNotificationIcon())
                        .setColor(0xff493C7C)
                        .setContentTitle(ctx.getString(R.string.app_name))
                        .setContentText(msg)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setLights(0xff493C7C, 1000, 1000)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(styledMsg));
执着的年纪 2024-11-17 02:10:16

我已经尝试了下面的代码,并且灯对我来说效果很好。我的手机是 Nexus 6P:

mBuilder.setContentTitle("APP_NAME")
                .setContentText(msg)
                .setContentIntent(PendingIntent.getActivity(mCtxt, UUID.randomUUID().hashCode(), new Intent(mCtxt, ReceivePushActivity.class), Notification.FLAG_AUTO_CANCEL))
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                //.setDefaults(Notification.DEFAULT_ALL)
                .setVibrate(new long[] {0, 1000, 200,1000 })
                .setLights(Color.MAGENTA, 500, 500)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.notify_logo);

        Notification ntf = mBuilder.build();
//        ntf.flags = Notification.DEFAULT_ALL;
//        ntf.flags = Notification.FLAG_ONLY_ALERT_ONCE;
//        ntf.flags = Notification.FLAG_AUTO_CANCEL;

        mNotificationManager.notify(notifyId, ntf);

意思是,删除“DEFAULT_ALL”设置。

I have tried my code below, and the light works fine for me. My mobile is Nexus 6P:

mBuilder.setContentTitle("APP_NAME")
                .setContentText(msg)
                .setContentIntent(PendingIntent.getActivity(mCtxt, UUID.randomUUID().hashCode(), new Intent(mCtxt, ReceivePushActivity.class), Notification.FLAG_AUTO_CANCEL))
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                //.setDefaults(Notification.DEFAULT_ALL)
                .setVibrate(new long[] {0, 1000, 200,1000 })
                .setLights(Color.MAGENTA, 500, 500)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.notify_logo);

        Notification ntf = mBuilder.build();
//        ntf.flags = Notification.DEFAULT_ALL;
//        ntf.flags = Notification.FLAG_ONLY_ALERT_ONCE;
//        ntf.flags = Notification.FLAG_AUTO_CANCEL;

        mNotificationManager.notify(notifyId, ntf);

Meaning, remove 'DEFAULT_ALL' settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文