You can use different tools and applications to set an alarm in your phone. Basically, setting up an alarm is sometimes important from your own Android Application. Suppose your app has a feature that will help users to remind to take medicine on a fixed time. This can also be done via other android features like service or AlarmManager. But if you want to make it simple just like all the operation of alarm, time, date, showing custom message then Android’s default AlarmClock will be best. In this tutorial we’ll learn how to set alarm in device using Java with the help of Intents.
The AlarmClock provider contains an Intent action and extras that can be used to start an Activity to set a new alarm or timer in an alarm clock application. Applications that wish to receive the ACTION_SET_ALARM and ACTION_SET_TIMER Intents should create an activity to handle the Intent that requires the permission com.android.alarm.permission.SET_ALARM.
Request Set Alarm Permission
Apps that need to set device alarm in clock must request permissions in Manifest. Android offers a permission called SET_ALARM. By requesting this permission, you can set alarm programmatically using Java codes with the help of Intent.
Add these permission in your AndroidManifest.xml like below:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.frsarker.setalarm"> <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now your application has the alarm setting permission.
Setting Alarm using AlarmClock & Intent
You may have already used intents before to start a new activity and passing data to another activity. But the usage of intent is not only limited there, you can use Intent to pass data to device settings, launching activities outside your app and so on. We will use intent now to pass a specific time to device’s alarm setting so that device will automatically set the alarm by itself, you won’t have to do it yourself.
You can set alarm using intents like below:
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM); i.putExtra(AlarmClock.EXTRA_HOUR, 18); i.putExtra(AlarmClock.EXTRA_MINUTES, 8); i.putExtra(AlarmClock.EXTRA_MESSAGE, "This is my custom message."); i.putExtra(AlarmClock.EXTRA_SKIP_UI, true); startActivity(i);
The code above will set an alarm at Today 06:08 PM. If the time given is past, then it will set the alarm on the given time tomorrow. Lets check which Bundle extras for what.
|EXTRA_HOUR||The hour of the alarm. The value is an Integer and ranges from 0 to 23. If not provided, an implementation should open an activity that allows a user to set a timer.|
|EXTRA_MINUTES||The minutes of the alarm. The value is an Integer and ranges from 0 to 59. If not provided, it defaults to 0.|
|EXTRA_MESSAGE||A custom message for the alarm or timer. The value is a String.|
|EXTRA_SKIP_UI||Whether or not to display an activity after performing the action. The value is a Boolean. The default is false.|
Now if you want to set an alarm which will be repeated on the same time on each Saturday, Monday & Friday then we can use another extra bundle called
EXTRA_DAYS. Lets check the code below:
ArrayList<Integer> alarmDays= new ArrayList<Integer>(); alarmDays.add(Calendar.SATURDAY); alarmDays.add(Calendar.MONDAY); alarmDays.add(Calendar.FRIDAY); Intent i = new Intent(AlarmClock.ACTION_SET_ALARM); i.putExtra(AlarmClock.EXTRA_HOUR, 18); i.putExtra(AlarmClock.EXTRA_MINUTES, 8); i.putExtra(AlarmClock.EXTRA_DAYS, alarmDays); i.putExtra(AlarmClock.EXTRA_MESSAGE, "This is my custom message."); i.putExtra(AlarmClock.EXTRA_SKIP_UI, true); startActivity(i);
The code above will set an alarm at 06:08 PM for each Saturday, Monday & Friday.