Q:

Add Alarm using AlarmClock Or Alarmmanager , From User Input

Am new to android app development
Need to turn on alarm from the user input data..
I have read about Alarmclock : Which can be used to set alarm to default clock app and it cannot be cancelled..
Alarmmanager : Used to trigger an event at specified time , but it has features of set or cancel the alarm
Idea: Using AlarmClock to set the alarm and cancel the alarm using Alarmmanager ..is it possible to do so 
How can i do it .? help me out

A:

To create the alarm you can do something like this:
        //... some code here

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // if you don't want the alarm to go off even in Doze mode, use
            // setExact instead
            mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                    alarmTimeMillis,
                    getPendingIntent(context);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP,
                    alarmTimeMillis,
                    getPendingIntent(context);
        } else {
            // Inexact time was only introduced in API 19+. Before that, all was exact time
            mAlarmManager.set(AlarmManager.RTC_WAKEUP,
                    alarmTimeMillis,
                    getPendingIntent(context);
        }

Some words here. Before API 19, all alarms were exact, that's why we use set method. On API 19 and after, inexact alarms were introduced, that's the reason why we need to use setExact for exact alarms and set for inexact alarms.
In API 23, Doze mode was introduced. In that mode, any alarm set with set or setExact will not go off if the phone is already in doze mode. To force the device out of Doze and trigger the alarm, use setExactAndAllowWhileIdle
The method getPendingIntent could be something like this:
    private PendingIntent getPendingIntent(Context context) {
        int requestCode = 69;
        return PendingIntent.getBroadcast(context, requestCode,
                new Intent(mContext, AlarmReceiver.class),
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

Of course, you can put extras into the intent as usual.
To cancel the alarm, you just need to create a pending intent with the same requestCode and call cancel on the alarm manager, something like:
(AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
                      .cancel(getPendingIntent(context));

Please mind that the call above is far from perfect so, do not use it as is.

