Reputation: 801
I have created a timer using Androids build in AlarmClock class using the below code
// This method is called to start the Timer from Androids Alarm Clock class
private void StartAlarmClockTimer()
{
Intent alarmIntent = new Intent(Android.Provider.AlarmClock.ActionSetTimer);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraLength, 5);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraVibrate, true);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraMessage, "Custom Text!");
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraSkipUi, true);
alarmIntent.AddFlags(ActivityFlags.NewTask);
StartActivity(alarmIntent);
}
It works and the timer functions correctly. The problem I have is how to cancel that timer prematurely. Should I somehow search for the timer and then dismiss it?
https://developer.android.com/reference/android/provider/AlarmClock.html
Upvotes: 0
Views: 231
Reputation: 9084
Since the AlarmClock
class doesn't expose a public action for this, there is no supported way to do this programmatically. The expectation is probably that users will do it via the UI. If you want more control, use AlaramManager and set/clear your own alarms.
Edit :
Usage like this :
AlarmManager manager;
Intent alarmIntent;
PendingIntent pendingIntent;
...
//Creat a timer
private void StartAlarmClockTimer()
{
manager = (AlarmManager)GetSystemService(Context.AlarmService);
alarmIntent = new Intent(Android.Provider.AlarmClock.ActionSetTimer);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraLength, 2);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraVibrate, true);
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraMessage, "Custom Text!");
alarmIntent.PutExtra(Android.Provider.AlarmClock.ExtraSkipUi, true);
alarmIntent.AddFlags(ActivityFlags.NewTask);
pendingIntent = PendingIntent.GetActivity(this, 0, alarmIntent, 0);
manager.SetExact(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime(), pendingIntent);
};
//Cancel a timer
private void CancelAlarmClockTimer()
{
manager.Cancel(pendingIntent);
}
Upvotes: 2