<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohammad Hossein Ilchi</title>
    <description>The latest articles on Forem by Mohammad Hossein Ilchi (@mohammad_hossein_ilchi).</description>
    <link>https://forem.com/mohammad_hossein_ilchi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1808208%2Fa197e9d5-7662-457c-9fa2-cf4d78284db5.jpeg</url>
      <title>Forem: Mohammad Hossein Ilchi</title>
      <link>https://forem.com/mohammad_hossein_ilchi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohammad_hossein_ilchi"/>
    <language>en</language>
    <item>
      <title>How To Schedule Exact Alarms⏱️In Android Using Java☕🧑‍💻</title>
      <dc:creator>Mohammad Hossein Ilchi</dc:creator>
      <pubDate>Mon, 19 Aug 2024 21:15:57 +0000</pubDate>
      <link>https://forem.com/mohammad_hossein_ilchi/how-to-schedule-exact-alarmsin-android-using-java-1alb</link>
      <guid>https://forem.com/mohammad_hossein_ilchi/how-to-schedule-exact-alarmsin-android-using-java-1alb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we will explore how to schedule exact alarms in Android using the Alarm Manager. Whether you're developing a To-Do app, an Alarm app, or any application that requires triggering actions or operations at precise times, understanding the Alarm Manager is crucial. This powerful tool allows you to set exact, inexact, and repeating alarms within your Android application. Throughout this article, we'll delve into the usage of the Alarm Manager library specifically for scheduling exact alarms in Java-based Android applications.&lt;br&gt;
Setting exact alarms in Android is very easy. Let's see how it works.&lt;/p&gt;

&lt;h1&gt;...&lt;/h1&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Let's Coding&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Setting alarms in Android is very simple, and there's no need to use external tools or libraries for this. Implementing it in your Android application is both easy and practical.&lt;/p&gt;
&lt;h2&gt;
  
  
  Permissions
&lt;/h2&gt;

&lt;p&gt;First, we need to declare some permissions in the Android Manifest file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.WAKE_LOCK"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; 
    &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.SCHEDULE_EXACT_ALARM"&lt;/span&gt; 
    &lt;span class="na"&gt;android:maxSdkVersion=&lt;/span&gt;&lt;span class="s"&gt;"32"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.USE_EXACT_ALARM"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;WAKE_LOCK:&lt;/strong&gt; This permission allows the app to continue working even when the device is asleep.&lt;br&gt;
&lt;strong&gt;SCHEDULE_EXACT_ALARM:&lt;/strong&gt; This permission is available for Android versions lower than 12 and allows the app to schedule exact alarms.&lt;br&gt;
&lt;strong&gt;USE_EXACT_ALARM:&lt;/strong&gt; This permission is required starting from Android 12 or higher, enabling the app to schedule exact alarms.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Broadcast Receiver
&lt;/h2&gt;

&lt;p&gt;Next, we need a broadcast receiver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AlarmReceiver&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;BroadcastReceiver&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onReceive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt; &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getStringExtra&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"123231"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeText&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Toast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LENGTH_LONG&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;show&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This broadcast receiver will display a Toast at the appointed time, with a title taken from the intent we send.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don't forget to declare the broadcast receiver in the manifest file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;receiver&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".AlarmReceiver"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Declare AlamManager &lt;/strong&gt;&lt;br&gt;
In the next part, we'll write the important portion of the code.&lt;br&gt;
First, we need an instance of the "AlarmManager" class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt; &lt;span class="n"&gt;alarmManager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getSystemService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ALARM_SERVICE&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need an Intent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Wake up"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Intent&lt;/span&gt; &lt;span class="n"&gt;intent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MainActivity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;this&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AlarmReceiver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;putExtra&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"123231"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we must create a "PendingIntent" to call the broadcast receiver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;PendingIntent&lt;/span&gt; &lt;span class="n"&gt;pendingIntent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBroadcast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
    &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FLAG_UPDATE_CURRENT&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FLAG_IMMUTABLE&lt;/span&gt;
&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To declare a PendingIntent, we need four parameters. The first parameter is the Context, the second is the PendingIntent's ID, the third is the Intent we declared above, and the fourth consists of two flags (you can read more about flags in the link below). Remember, for Android 12 or higher, you must use either &lt;em&gt;PendingIntent.FLAG_IMMUTABLE&lt;/em&gt; or &lt;em&gt;PendingIntent.FLAG_MUTABLE&lt;/em&gt; to declare the PendingIntent.&lt;br&gt;
&lt;a href="https://developer.android.com/reference/android/app/PendingIntent#constants" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, set the time in the AlarmManager:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;triggerAtMillis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentTimeMillis&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;alarmManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setExact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RTC_WAKEUP&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;triggerAtMillis&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pendingIntent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use these three methods to set the time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.setExact():&lt;/code&gt; This method is used to invoke an alarm at a nearly precise time in the future. However, it does not work when battery-saving mode is enabled.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.setExactAndAllowWhileIdle():&lt;/code&gt; This method works like setExact(), but battery-saving mode has no effect on it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.setAlarmClock():&lt;/code&gt; This method can set exact alarms like setExact() and works even when battery-saving mode is active. Additionally, the system identifies these alarms as the most critical ones and exits low-power modes if necessary to deliver the alarms.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These methods require three parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The type of alarm. In Android, we can use the following types:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ELAPSED_REALTIME&lt;/code&gt;: Setting an alarm based on the elapsed time since the device booted, without waking the device from sleep.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ELAPSED_REALTIME_WAKEUP&lt;/code&gt;: Setting an alarm based on the elapsed time since the device booted, and waking the device from sleep to execute the alarm.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RTC&lt;/code&gt;: Fires the pending intent at the specified time but does not wake up the device.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RTC_WAKEUP&lt;/code&gt;: Wakes up the device to fire the pending intent at the specified&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;The second parameter is the time to call the pending intent (alarm time).&lt;/li&gt;
&lt;li&gt;The third parameter is the PendingIntent that we declared above.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Congratulations, you have created an Alarm Manager.&lt;br&gt;
I hope you enjoyed this article and found it helpful.&lt;/p&gt;

</description>
      <category>android</category>
      <category>java</category>
      <category>programming</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
