Search code examples
androidflags

Flagging issues in android


I'm trying to get my Notification to not cancel when the user presses "Clear All" So far I have the intent working properly on everything except this:

        Intent intent = new Intent(getBaseContext(), MainActivity.class);
    intent.addFlags(Notification.FLAG_ONGOING_EVENT);
    intent.addFlags(Notification.FLAG_NO_CLEAR);
    PendingIntent contentIntent = PendingIntent.getActivity(
            getBaseContext(), 0, intent, 0);  

The question I have at this point is: Are my flags correct?


Solution

  • Yes, your flags look pretty much correct, although I don't know if you even need the FLAG_NO_CLEAR. I currently have an app which creates an ongoing (non-cancellable) notification - I only use the FLAG_ONGOING_EVENT and it works fine for me. I pretty much just copied it from a tutorial and then added the ongoing event flag. Here's some sample code:

    String text = "notification";
    Notification notification = new Notification(R.drawable.icon, text,
            System.currentTimeMillis());
    
    //launch the activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent().setComponent(ComponentName.unflattenFromString("com.example/com.example.MyActivity")), 0);
    
    // set the label and text...
    notification.setLatestEventInfo(this, getText(R.string.notification_label),
                   text, contentIntent);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    
    // Send the notification.
    // We use a string id because it is a unique number.  We use it later to cancel.
    NotificationManager mNM;
    mNM.notify(R.string.notification_label, notification);