Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

alarmmanager - Android Alarm Clock - Remove alarms programmatically

I am writing an application in which user can configure alerts/alarms. At this point, I have everything working expect the one. I am firing off an alarm using

Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, "Some message!");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, calendar.get(Calendar.HOUR_OF_DAY));
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, calendar.get(Calendar.MINUTE));
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
context.startActivity(alarmIntent);

I want to remove this alarm once user has dismissed using the Dismiss button. I can see the alarms being still there in the alarm clock which I set using above code through my application.

Is there some way to get a cursor or something similar on the alarms being there in the alarm clock? This will help me iterate over them and remove the ones I want.

Any help would be appreciated, Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As it was answered here: there is no supported API to this.

Official documentation says that

If a time of day is specified, and EXTRA_SKIP_UI is true, and the alarm is not repeating, the implementation should remove this alarm after it has been dismissed.

But different manufactures implement their own alarm clocks and I'm not sure if someone supporrts this. I have tried it on JB emulator and looks like it dosen't work. Maybe, on KitKat? Only this one is ok on JB:

If an identical alarm exists matching all parameters, the implementation may re-use it instead of creating a new one

So, maybe, better solution is to use youre own alarm, created with AlarmManager or warn user that he has to remove Alarms by hands (to make it more obvious - not use AlarmClock.EXTRA_SKIP_UI).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...