29

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

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

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

4

8 回答 8

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

除了使用 ARGB 值作为示例显示,您还可以在android.graphics.Color类中使用 int 属性来获取颜色(例如Color.WHITE

于 2011-06-09T12:25:47.530 回答
11

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

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

于 2011-06-04T04:39:40.547 回答
11

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

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

于 2014-06-12T08:31:20.767 回答
7

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

但是在这个新的实现中,您可能无法控制LED 开毫秒LED 关毫秒,这将取决于硬件。

于 2017-08-21T09:18:48.687 回答
2

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

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

于 2011-05-29T18:04:31.847 回答
2

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

于 2011-06-08T22:13:11.967 回答
2

看看下面的来源。

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));
于 2016-11-15T13:15:11.587 回答
1

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

于 2017-01-09T01:44:06.723 回答