Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Status Bar Notification: make it un-clearable and have it return to app (not start a new instance)

I am developing an Android app and I want a status bar notification that cannot be cleared by the user. Does anyone know how to do that? I've seen them with apps like Skimble where a non-clearable notification appears while using the application.

Also, I'd when the user clicks/presses the notification, I want it to return to the app instance that is already running. Right now it starts a new instance. Again like the Skimble app, I just want to return to the already running instance of the app.

Thanks.

like image 466
Damon Avatar asked Jul 28 '11 21:07

Damon


People also ask

What is a status bar notification bar on Android phone and how it works?

Status bar (or notification bar) is an interface element at the top of the screen on Android devices that displays the notification icons, minimized notifications, battery information, device time, and other system status details.

What is permanent notification?

A permanent notification is a (usually silent) alert displayed by certain apps on your Android smartphone or tablet at all times to let you know they're running in the background.


1 Answers

I found a solution to this problem. I used FLAG_ONGOING_EVENT to make the notification persist. Here's the code:

  String ns = Context.NOTIFICATION_SERVICE;
  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.mypic;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    notification.flags = Notification.FLAG_ONGOING_EVENT;

    Context context = getApplicationContext();
    CharSequence contentTitle = "Title Text";
    CharSequence contentText = "Content text.";

    Intent intent = new Intent(this, MyClass.class); 
    intent.setAction("android.intent.action.MAIN"); 
    intent.addCategory("android.intent.category.LAUNCHER"); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
like image 138
Damon Avatar answered Nov 14 '22 22:11

Damon