Home » Android » java – Set android alarm clock programmatically

java – Set android alarm clock programmatically

Posted by: admin May 14, 2020 Leave a comment


I’m trying to build a personal app that will set alarms in the DeskClock app. I can get it to set alarms for anytime in the current day, But how would I go about setting an alarm for the next day, or later in the week. Looking through the AlarmClock api in android I don’t see a normal way to do this. Is this even possible?

Btw this is my code for setting the alarm it might not be pretty, but I am learning as I go.

 package com.netwokz.setit;

    import java.util.Calendar;
    import java.util.GregorianCalendar;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.provider.AlarmClock;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class MainActivity extends Activity implements OnClickListener {
    Button btnSetAlarm;
    EditText etHour, etMinute;
    int minute, hour, day;
    Calendar cal;

    protected void onCreate(Bundle savedInstanceState) {
        btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
        etHour = (EditText) findViewById(R.id.etHour);
        etMinute = (EditText) findViewById(R.id.etMinute);


    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity, menu);
        return true;

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_set_alarm:

    private void setAlarm() {
        cal = new GregorianCalendar();
        day = cal.get(Calendar.DAY_OF_WEEK);
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);

        Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
        i.putExtra(AlarmClock.EXTRA_HOUR, hour + Integer.parseInt(etHour.getText().toString()));
        i.putExtra(AlarmClock.EXTRA_MINUTES, minute + Integer.parseInt(etMinute.getText().toString()));
        i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
How to&Answers:


public class MainActivity extends Activity {

    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    TextView textAlarmPrompt;
    TimePickerDialog timePickerDialog;

    final static int RQS_1 = 1;

    public void onCreate(Bundle savedInstanceState) {

        textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);

        buttonstartSetDialog = (Button) findViewById(R.id.startAlaram);
        buttonstartSetDialog.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {



    private void openTimePickerDialog(boolean is24r) {
        Calendar calendar = Calendar.getInstance();

        timePickerDialog = new TimePickerDialog(MainActivity.this,
                onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), is24r);
        timePickerDialog.setTitle("Set Alarm Time");



    OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if (calSet.compareTo(calNow) <= 0) {
                // Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);


    private void setAlarm(Calendar targetCal) {

        textAlarmPrompt.setText("\n\n***\n" + "Alarm is set "
                + targetCal.getTime() + "\n" + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),



public class AlarmReceiver extends BroadcastReceiver {

    public void onReceive(Context k1, Intent k2) {
        // TODO Auto-generated method stub
         Toast.makeText(k1, "Alarm received!", Toast.LENGTH_LONG).show();




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp" >

        android:text="Set Alaram Time" />

        android:textColor="#000000" />



        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
         <receiver android:name=".AlarmReceiver" android:process=":remote" />


Like a real alarm clock, I don’t see a way to set an alarm for a future date. I think this requires one of two options:

  1. A combination of AlarmManager (to trigger an app service on the desired future date to create the alarm in AlarmClock) and then AlarmClock (to actually handle the alarm). Don’t forget to listen for BOOT_COMPLETED broadcast to reset the AlarmManager alarms since they don’t persist after a reboot.

  2. Use AlarmManager to trigger an app service and UI that simulates an alarm. This will require you to consider phone state and etc things you don’t have to worry about with AlarmClock.

I think you can make your app just bring up the AlarmClock dialog which allows the user to set recurring alarms and other options you cannot perform through the intent.


How about trying a pending Intent?

Just change the Calendar value to a few days or so in advance?

AlarmManager am = (AlarmManager)getSystemService(alarm);

Intent i= new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 2);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);