Unable to disable / hide the alarm icon on Android 5.0 Lollipop

Since the release of Android Lollipop 5.0 (API21), there is now an API that officially shows / hides the alarm icon. More information about this is here in stackoverflow.

Thanks to this, I was able to display the alarm icon on 5.0 + Android devices. Unfortunately, I cannot remove / hide / cancel the icon if the alarm is disabled.

Here is what I am doing (a combination of several attempts from Stackoverflow and Android stock):

public static void setNextAlert(final Context context) {

final Alarm alarm = calculateNextAlert(context);

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);

PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

int flags = alarm == null ? PendingIntent.FLAG_NO_CREATE : 0;
PendingIntent operation = PendingIntent.getBroadcast(context, 0 /* requestCode */,  intent, flags);


if (alarm != null) 
{
    if(UtilsAlarm.isLollipopOrLater())
    {
        PendingIntent viewIntent = PendingIntent.getActivity(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(alarm.time, viewIntent);
        am.setAlarmClock(info, operation);
    }
    else
    {
        if(UtilsAlarm.isKitKatOrLater())
        {
            am.setExact(AlarmManager.RTC_WAKEUP, alarm.time, sender);
        }
        else
        {   
            am.set(AlarmManager.RTC_WAKEUP, alarm.time, sender);
        }

        setStatusBarIcon(context, true);
    }

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(alarm.time);
    String timeString = formatDayAndTime(context, c);
    saveNextAlarm(context, timeString);
} 
else 
{
    if(UtilsAlarm.isLollipopOrLater())
    {
        am.cancel(operation);
    }
    else
    {
        am.cancel(sender);
        setStatusBarIcon(context, false);
    }

    saveNextAlarm(context, "");
}

Intent i = new Intent(NEXT_ALARM_TIME_SET);
context.sendBroadcast(i); }

, Lollipop, . , Lollipop , , ( ), ( ), .

- ?

Thansk .

+4
1

int flags = alarm == null ? PendingIntent.FLAG_NO_CREATE : 0;

null PendingIntent operation, , (. doc FLAG_NO_CREATE).

, am.cancel(operation);, , operation == null. . , , , , ( ) : PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);. , : " ( )". ; , .

FLAG_UPDATE_CURRENT.

+1

All Articles