【问题标题】:Changing LED color for notifications更改通知的 LED 颜色
【发布时间】:2017-10-13 07:10:01
【问题描述】:

我基本上只是在尝试 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);
    }
  };
}

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

谁能看到我的代码有什么问题,或者知道我是否需要做其他事情来实现这一点?

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    你可以使用这个代码:

    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

    【讨论】:

    • 我知道,我把它全部写给了你,这样你就可以在你的代码中做出改变。可能会奏效。因为它适用于我的设备
    • 好吧,它不起作用,我已经尝试过(在我问这个问题之前)......而且,由于 LED 在不同设备上的工作方式不同,你可能还没有在与我的设备相同,它在您的设备上运行并不是特别有用 - 此代码可能适用于许多设备,但不是我的 - 这就是问题所在。
    • 感谢 Stuti - 对我来说效果很好,我遇到了与海报完全相同的问题(绿灯闪烁)。 Frxstrem 我建议您检查您正在使用的 ARGB 代码并与 Stuti 编写的代码进行比较..
    • LED_NOTIFICATION_ID 呢?它应该是一个内置常量还是什么?谢谢,
    • 不,LED_NOTIFICATION_ID?是一个局部变量。可以设置为:int LED_NOTIFICATION_ID = 0;
    【解决方案2】:

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

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

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    【解决方案4】:

    FLAG_SHOW_LIGHTSNotification.Builder.setLights(int,int,int); 自 Android O(API 级别 26)起已弃用如果您计划在 API 级别 26 或更高级别中使用此功能,请查看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);
    

    但在这个新实现中,您可能无法控制 LED on milli-secondsLED off milli-seconds,这将取决于硬件。

    【讨论】:

    • 如果您的目标是较低的 API 级别,您还可以使用旧方法吗?
    • 从 Android 8.0(API 级别 26)开始引入,适用于较低版本的 Android,您可能必须使用较旧的 API
    【解决方案5】:

    尝试使用十六进制颜色,包括一个 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.
    

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

    【讨论】:

    • 我已经尝试将notification.defaults 设置为0notification.ledARGB 设置为十六进制值(即使Color.BLUE 具有0xff0000ff 的常量值),但这些都不是工作。我也知道硬件可以做近似,但我确信它至少应该能够做绿色、蓝色、橙色和粉红色的颜色。
    • 很奇怪。 Go SMS Pro 应用程序能否成功更改您手机上的颜色?我知道有些手机的 LED 通知颜色变化存在问题。
    • 是的,至少对于该应用程序中预设的颜色(正如我提到的,绿色、蓝色、橙色和粉红色)是这样。但是,我尝试了一些其他应用程序,这些应用程序应该改变 LED 的颜色,但它们不起作用,并且无论它们设置为什么颜色,最终都会显示绿色闪烁的灯,就像我的应用程序一样。
    • 我最近注意到小米 A3 通知 LED 是单色的。我想知道这是否会使应用程序崩溃或系统会尝试“最佳”近似值。大声笑。
    【解决方案6】:

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

    【讨论】:

    • 嗯,我非常有信心 USB、其他应用程序和屏幕都不会导致这种情况,因为我确实得到了一个闪烁的灯,尽管它不是我要求的闪烁率或颜色。我认为这是代码本身的问题,因为我自己的代码以及我在市场上找到的许多应用程序等都存在我描述的问题,而其他一些应用程序则没有,并且确实有效。
    • 这是运行 2.3.4 库存的 Nexus One,还是其他?
    【解决方案7】:

    看看下面的源代码。

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

    【讨论】:

      【解决方案8】:

      我已经尝试了下面的代码,并且灯对我来说很好用。我的手机是 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”设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-23
        • 1970-01-01
        • 2014-04-14
        • 2014-09-04
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 2022-11-03
        相关资源
        最近更新 更多