Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post a notification with no icon but with led enabled?
    text
    copied!<p>Hi fellow Android coders !</p> <p>I'm currently writing an application that uses the new Android 4.3 notification listener to change the notification led color when a notification is received, and am facing a problem, probably due to my lack of knowledge about how notifications work.</p> <p>So far, it is working quite well. When the user switch off the screen, I create a notification, with a custom led color, and I remove it when the screen is switched ON. My problem is that when the user switch the screen back ON, he can see my notification icon on the status bar for half a second just before it is removed. Not that big of a deal, but as a nit-picker, I can't help but try a way to avoid this ugly behavior. I know that some apps succeed as doing this - LightFlow for example.</p> <p>My first idea was to play with the priority of the notification and using Notification.PRIORITY_MIN nearly worked : the notification icon is not displayed on the status bar but appears when the bar is expanded.</p> <p>I unfortunately discovered that notifications with minimum priority doesn't toggle the notification led when the screen is off !</p> <p>I then tried to create a notification with no icon, but it is not supported by the framework - that's actually a good thing.</p> <p>And now I'm out of idea.</p> <p>Can someone help my find a way to create a notification that doesn't appear on the status bar but still toggles the LED on ?</p> <p>Or maybe should I remove my notification <em>before</em> the screen is actually switched ON but I can't find a way to do this either...</p> <p>If someone can help me on this one, it would make my day !</p> <p>Here is the code source of my application :</p> <h1>Main activity :</h1> <pre><code>package com.nightlycommit.coloration; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } </code></pre> <h1>Service listener</h1> <pre><code>package com.nightlycommit.coloration; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.service.notification.StatusBarNotification; /** * Created by Eric on 08/08/13. */ public class NotificationListenerService extends android.service.notification.NotificationListenerService { private final String TAG = getClass().getSimpleName(); @Override public void onCreate() { super.onCreate(); // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new ScreenStateReceiver(); registerReceiver(mReceiver, filter); } @Override public void onNotificationPosted(StatusBarNotification sbn) { String text = "Notification posted !"; } @Override public void onNotificationRemoved(StatusBarNotification sbn) { String text = "Notification removed !"; } } </code></pre> <h1>Screen state receiver :</h1> <pre><code>package com.nightlycommit.coloration; import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.graphics.Color; /** * Created by Eric on 12/08/13. */ public class ScreenStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); String action = intent.getAction(); if (action != null) { if (action.equals(Intent.ACTION_SCREEN_OFF)) { Notification.Builder builder = new Notification.Builder(context); builder.setLights(Color.argb(255,255,0,0), 500, 500); builder.setContentText("TEST"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setOngoing(true); // builder.setPriority(Notification.PRIORITY_MIN); notificationManager.notify(777, builder.build()); } else if (action.equals(Intent.ACTION_SCREEN_ON)) { notificationManager.cancel(777); } } } } </code></pre> <p>Thanks by advance,</p> <p>Eric.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload