Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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
Android 更改通知的LED颜色_Android_Android Notifications - Fatal编程技术网

Android 更改通知的LED颜色

Android 更改通知的LED颜色,android,android-notifications,Android,Android Notifications,我基本上只是在尝试Android开发,几天前我发现了一个名为“”的应用程序,它可以设置不同颜色(蓝色、绿色、橙色、粉色和浅蓝色)的通知。因此,我尝试在自己的应用程序中实现这一点,但我无法更改LED的颜色或闪烁的内部。我目前使用以下代码: public class MainActivity extends Activity { static final int NOTIFICATION_ID = 1; @Override public void onCreate(Bundle sav

我基本上只是在尝试Android开发,几天前我发现了一个名为“”的应用程序,它可以设置不同颜色(蓝色、绿色、橙色、粉色和浅蓝色)的通知。因此,我尝试在自己的应用程序中实现这一点,但我无法更改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);
    }
  };
}
但正如我所说的,它没有按照我想要的方式工作;相反,它只是以常规绿色闪烁,默认延迟,而不是我在代码中设置的延迟


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

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

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

LED在android手机中是非常不标准的功能。如果你依赖于它们,你将错过大量的用户群(例如,考虑SGS手机,它甚至没有LED)


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

对LED颜色的支持非常不稳定。尝试拔下USB电缆,确保没有其他应用程序同时尝试修改LED。同时关闭屏幕。

您可以使用以下代码:

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值(例如)。

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


在S3、N5、N4和NexusOne上也可以正常工作。

请查看下面的源代码

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

我已经尝试了下面的代码,灯光对我来说很好。我的手机是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);
这意味着,如果您计划在API级别26或更高的版本中使用此功能,请删除“DEFAULT_ALL”设置。

并且由于Android O(API级别26)而被弃用

:


但是在这个新的实现中,您可能无法控制它&它将依赖于硬件。

我尝试了设置
通知。默认设置
0
通知。ledARGB
为十六进制值(即使
Color.BLUE
具有
0xff0000ff
的常量值),但这两件事都不管用。我也知道硬件可以做近似,但我确信它至少可以做绿色、蓝色、橙色和粉色。很奇怪。Go SMS Pro应用程序是否成功更改手机上的颜色?我知道有些手机会出现LED通知颜色变化的问题。是的,至少在该应用程序中预设的颜色(如我所述,绿色、蓝色、橙色和粉色)会出现问题。然而,我尝试过其他一些应用程序,这些应用程序应该会改变LED的颜色,但不起作用,不管它们设置为什么颜色,最终都会显示绿色闪烁的灯光,就像我的应用程序一样。我最近注意到小米A3通知LED是单色的。。我不知道这是否会导致应用程序崩溃,或者系统会尝试“最佳”近似值。哈哈,我很有信心,无论是USB、其他应用程序还是屏幕都不会造成这种情况,因为我确实得到了一个闪烁的灯光,尽管这不是我所要求的闪烁频率或颜色。我认为这是代码本身的一部分,因为我自己的代码,以及我在市场上发现的许多应用程序等,都有我描述的问题,而其他一些应用程序则没有,并且实际上可以工作。这是运行2.3.4股票的Nexus One还是其他什么?我知道,我把整个代码都写给你,这样你就可以在代码中做出改变。也许它有用。因为它在我的设备上工作,所以它不工作,我已经尝试过了(在我提问之前)…而且,由于LED在不同的设备上工作方式不同,而且您可能没有在我的设备上测试过它,所以它在您的设备上工作并没有特别大的帮助-此代码可能在许多设备上工作,但不是我的-这就是问题所在。谢谢Stuti-对我来说效果很好,我的问题与海报上的问题完全一样(绿灯闪烁)。Frxstrem我建议您检查正在使用的ARGB代码,并与Stuti编写的代码进行比较。LED_NOTIFICATION_ID呢?它应该是一个内置常量还是什么?谢谢,不用了,LED\u通知\u ID?是一个局部变量。您可以将其设置为:int-LED\u-NOTIFICATION\u-ID=0;我想实现这一点,请指导我如何实现。仅供参考,自API级别26以来,它已被弃用。请参阅下面我的答案:如果您的目标是较低的API级别,您仍然可以使用旧方法吗?由于它是自Android 8.0(API级别26)开始为较低版本的Android引入的,您可能必须使用较旧的API
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);