更改通知的LED颜色

Frx*_*rem 28 android android-notifications

我基本上只是尝试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);
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

但正如我所说,它并不像我想要的那样工作; 相反,它只是在常规绿色中以默认延迟闪烁,而不是我在代码中设置的那个.

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

Stu*_*uti 12

您可以使用此代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

您可以在android.graphics.Color类中使用int属性来获取颜色,而不是使用ARGB值作为示例节目(例如Color.WHITE)

  • 嗯,它不起作用,我已经尝试过(在我提出问题之前)...而且,由于LED在不同设备上的工作方式不同,而且你可能没有在同一设备上测试它,它不是特别有用的是它可以在您的设备上运行 - 这个代码可能适用于许多设备,但不适用于我的 - 这就是问题所在. (3认同)
  • LED_NOTIFICATION_ID怎么样?它应该是内置常量还是什么?谢谢, (2认同)

The*_*Man 11

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

也可以在S3,N5,N4,Nexus上正常使用..


Ale*_*dam 10

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

也就是说,id int字段ledARGB没用,你可能需要从该APK调查一些JNI调用.我的猜测是它会有不同的方法,具体取决于运行的设备.


pcj*_*pcj 6

FLAG_SHOW_LIGHTS Notification.Builder.setLights(int,int,int); 从Android O(API级别26)开始不推荐使用。如果您打算在API级别26或更高版本中使用此功能,请查看 NotificationChannel

范例

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);
Run Code Online (Sandbox Code Playgroud)

但在这个新的实现您可能没有超过控制在毫秒LEDLED熄灭毫秒这将是依赖于硬件。


Fem*_*emi 2

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

notification.defaults = 0;
notification.ledARGB = 0xff0000ff;
Run Code Online (Sandbox Code Playgroud)

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

public int ledARGB
Since: API Level 1

The color of the led. The hardware will do its best approximation.
Run Code Online (Sandbox Code Playgroud)

我假设您的硬件具有所有颜色,但事实可能并非如此。