Android - Creating Intents: Alarm Clock
Hello all,
I really enjoyed the Android Basics with Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Alarm Clock.
First I just created a simple button to activate the intent which looks like this:
![Android App showing a single button called ALARM.](https://static.wixstatic.com/media/b86045_406680d9a4c84ca5848fc82f89748663~mv2.png/v1/fill/w_301,h_499,al_c,q_85,enc_auto/b86045_406680d9a4c84ca5848fc82f89748663~mv2.png)
And the code in activity_main.xml looks like this:
<Button
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_gravity="center"
android:layout_margin="@dimen/margin"
android:onClick="createAlarm" android:text="Alarm"/>
Then I created an activity in MainActivity.java to call the intent when the button is clicked. To launch a clock app all you need to do is include this code.
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
However, there are also other lines of code you can add to pre-set certain aspects of the alarm. I experimented with a number of these and ended up with some code that looks like this, with explanatory comments:
public void createAlarm(View view) {
//This creates an ArrayList called alarmDays which is passed into the .EXTRA_DAYS method
ArrayList<Integer> alarmDays = new ArrayList<>();
//This sets Monday as one of the alarm days
alarmDays.add(2);
//This sets Tuesday as one of the alarm days
alarmDays.add(3);
//This sets Wednesday as one of the alarm days
alarmDays.add(4);
//How to set up a new Alarm intent
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
//This sets which days the alarm goes off on using the ArrayList above
.putExtra(AlarmClock.EXTRA_DAYS, alarmDays)
//This sets the name or message of the alarm
.putExtra(AlarmClock.EXTRA_MESSAGE, "Wake up!")
//This sets the hour of the alarm (24 hour) to 7am
.putExtra(AlarmClock.EXTRA_HOUR, 7)
// This sets the minutes of the alarm to 30
.putExtra(AlarmClock.EXTRA_MINUTES, 30)
// This sets the alarm to (false=not) vibrate
.putExtra(AlarmClock.EXTRA_VIBRATE, false)
// This is meant to make the alarm be silent but I don't think it works...
.putExtra(AlarmClock.EXTRA_RINGTONE, "VALUE_RINGTONE_SILENT");
// To show the Alarm app after adding an alarm, uncomment the line below:
//intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
// This line first checks that your phone has an app that can handle the intent before
// starting so the app doesn't crash if your phone doesn't an an appropriate app
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
The final thing you need is to get permission to set an alarm. To do this you need to go to Apps > Manifest > AndroidManifest.xml…
![Android Studio sub-menu showing where you can find the AndroidManifest.xml file](https://static.wixstatic.com/media/b86045_25a686b419c4489b8acea74713b624e1~mv2.png/v1/fill/w_493,h_343,al_c,q_85,enc_auto/b86045_25a686b419c4489b8acea74713b624e1~mv2.png)
The code originally looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.intentspractise">
<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>
You need to add this extra line of code INSIDE the manifest section but OUTSIDE all other sections of this file. That new line looks like this:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
The updated file should therefore look like this, with comments:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.intentspractise">
<!--The line below has been changed to give this app permission to set the alarm
N.B. The permission line HAS to be put here, inside the manifest section but outside the
other sections -->
<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>
All of this should allow you to use the Alarm Clock intent in your app and modify it in a number of ways.
I welcome any comments or questions about this, especially corrections of anything I can improve. For example, I am not convinced that I have used VALUE_RINGTONE_SILENT correctly and I feel that I should have been able to populate the alarmDays ArrayList without having to push each value separately. I was also unable to work out how to change the alarm tone using EXTRA_RINGTONE.
Links: