2011-05-29 65 views
24

我基本上只是试验Android开发,几天前我遇到了这个名为“Go SMS Pro”的应用程序,其中可以设置不同颜色的通知(蓝色,绿色,橙色,粉红色和浅蓝色)。所以,我试图在我自己的应用程序中自己做这件事,但是我无法改变LED的颜色和闪烁的内部。我目前使用此代码:更改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); 
    } 
    }; 
} 

但正如我所说,它不工作,我想要的方式;相反,它只是默认延迟闪烁,而不是我在代码中设置的那个。

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

回答

2

尝试使用十六进制颜色,包括阿尔法值和默认值设置为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. 

我假设你的硬件有所有的颜色,但它可能不是。

+0

我已经尝试将'notification.defaults'设置为'0'和'notification.ledARGB'设置为十六进制值(尽管'Color.BLUE'具有常量值'0xff0000ff'),但都没有事情工作。我也知道硬件可以做近似,但我确信它至少应该能够做到绿色,蓝色,橙色和粉红色。 – Frxstrem 2011-05-29 18:08:29

+0

很奇怪。 ** Go SMS Pro **应用程序是否成功更改手机上的颜色?我知道一些手机有LED通知颜色变化的问题。 – Femi 2011-05-29 18:12:16

+0

是的,它的确如此,至少对应于该应用程序中预设的颜色(如我所提到的,绿色,蓝色,橙色和粉红色)。但是,我尝试了一些其他应用程序,这些应用程序应该会改变LED的颜色,但不起作用,并且最终会显示绿色闪烁的光,而不管它们设置了什么颜色,就像我的应用程序一样。 – Frxstrem 2011-05-29 18:14:31

10

在安卓手机中,LED是非常不标准的功能。如果你依赖他们,你将错过很多用户群(例如,考虑甚至没有LED的SGS电话)。

这就是说,id int字段ledARGB没有用处,您可能需要查看该APK的某个JNI调用。我的猜测是,它将有不同的方法,取决于运行的设备。

+0

我想实现这个https://stackoverflow.com/questions/25479517/change-notification-led-color-if-is-previously-set-by-the-creator-application请指导我如何做到这一点。 – 2014-08-25 09:30:20

1

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

+0

嗯,我非常有信心,USB,其他应用程序和屏幕都没有造成这种情况,因为我确实得到了闪烁的灯光,尽管它不是闪烁的速度,也不是我所要求的颜色。我认为这是代码本身的东西,因为我自己的代码和我在市场上找到的许多应用程序都有我描述的问题,而其他一些应用程序却没有,并且实际上工作。 – Frxstrem 2011-06-09 14:28:32

+0

这是一个运行2.3.4股票或其他东西的Nexus One? – 2011-06-09 18:28:08

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值作为例子显示,你可以使用int属性里面android.graphics.Color类来获取颜色,以及(如Color.WHITE

+0

除了一些细微的差异,这基本上**是**我正在使用的代码... – Frxstrem 2011-06-09 14:23:58

+0

我知道,我写了整个给你,以便你可以在你的代码中的差异。可能是有效的。因为它在我的设备上工作 – Stuti 2011-06-09 16:33:49

+3

嗯,它不工作,我已经尝试过(在我问这个问题之前)...而且,由于LED在不同设备上的工作方式不同,并且您可能没有在相同的设备上进行测试设备,因为它在你的设备上工作并不是特别有用 - 这个代码可能适用于很多设备,但不是我的 - 这就是问题所在。 – Frxstrem 2011-06-09 16:49:00

11

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

在S3,N5,N4,Nexus上也可以正常工作..

+0

仅供参考,因为API已被弃用26级看到我的答案如下:https://stackoverflow.com/questions/6169291/changing-led-color-for-notifications/45793450#45793450 – pcj 2017-08-21 09:31:14

1

看一看源代码如下。

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

我已经尝试过我的代码,灯光对我来说工作正常。我的手机是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'设置。

2

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-seconds & LED off milli-seconds它将取决于硬件。

+0

如果你的目标是较低的API级别,你仍然可以使用旧的方法吗? – Michael 2017-09-18 00:11:27

+0

从Android 8.0(API级别26)开始,针对较低版本的Android,您可能必须使用旧版API – pcj 2017-09-18 05:56:31