tienen que para notificaciones notificacion luz frontal como color celulares cambiar activar android android-notifications

android - que - Cambio de color LED para notificaciones.



led frontal para notificaciones (8)

¿Intentaste: .setLights (Color.BLUE, 500, 500)?

Funciona bien en S3, N5, N4, Nexus one también ...

Básicamente estoy experimentando con el desarrollo de Android, y hace un par de días encontré esta aplicación llamada " Go SMS Pro ", que, entre otras cosas, puede configurar notificaciones en diferentes colores (azul, verde, naranja, rosa y luz). azul). Por lo tanto, he intentado hacerlo yo mismo en mi propia aplicación, sin embargo, no puedo cambiar ni el color ni el parpadeo interno del LED. Actualmente utilizo este código:

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

Pero como dije, no funciona como quiero que lo haga; en su lugar, solo parpadea en verde normal con el retraso predeterminado, y no el que he establecido en mi código.

¿Alguien puede ver lo que está mal con mi código o saber si tengo que hacer algo más para lograrlo?


Echa un vistazo a la fuente a continuación.

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


El soporte para los colores LED es realmente irregular. Intente desconectar su cable USB y asegúrese de que ninguna otra aplicación intente modificar el LED al mismo tiempo. También apaga la pantalla.


He probado mi código a continuación, y la luz funciona bien para mí. Mi móvil es 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);

Es decir, eliminar la configuración ''DEFAULT_ALL''.


Intente usar el color hexadecimal, incluya un valor alfa y establezca los valores predeterminados en 0:

notification.defaults = 0; notification.ledARGB = 0xff0000ff;

Además, la interfaz de notificación dice esto:

public int ledARGB Since: API Level 1 The color of the led. The hardware will do its best approximation.

Supongo que su hardware tiene todos los colores, pero puede que no.


Los leds son una característica bastante no estándar en los teléfonos Android. Si depende de ellos, se perderá una buena parte de la base de usuarios (considere, por ejemplo, los teléfonos SGS, que ni siquiera tienen leds).

Dicho esto, id el campo int ledARGB no fue útil, es posible que debas buscar en alguna llamada JNI de ese APK. Mi conjetura es que tendrá diferentes métodos dependiendo del dispositivo en el que se está ejecutando.


Puedes usar este código:

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

En lugar de usar el valor ARGB como muestra el ejemplo, puede usar la propiedad int dentro de la clase android.graphics.Color para obtener el color también (por ejemplo, Color.WHITE )


FLAG_SHOW_LIGHTS y Notification.Builder.setLights(int,int,int); están en desuso desde Android O (nivel de API 26) Si planea usar esta función en el nivel de API 26 o superior, consulte 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);

Pero en esta nueva implementación, es posible que no tenga control sobre el LED en milisegundos y el LED apagado milisegundos , dependerá del hardware.