Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>Intent intent = new Intent(this, MyReceiver.class); intent.putExtra("key", "Alert"); pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0); alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar=Calendar.getInstance(); // Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second) calendar.set(2013, Calendar.OCTOBER, 13, 18, 55, 40); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent); </code></pre> <p>This param is interval in milliseconds between subsequent repeats of the alarm:</p> <blockquote> <p>5*1000 = 5 second</p> </blockquote> <p>Sample: </p> <blockquote> <p>1year = 365(day)* 24(hour)* 60(min)* 60(second)* 1000(sec -> milisecond);<br> // leap year 366day</p> </blockquote> <p>The month value is 0-based, so it may be clearer to use a constant like <code>Calendar.OCTOBER</code></p> <p>MyReceiver.java</p> <pre><code>public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,intent.getStringExtra("key"),Toast.LENGTH_SHORT).show(); } } </code></pre> <p><strong>update</strong>: Note: Beginning with API 19 (Build.VERSION_CODES.KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. <a href="https://developer.android.com/reference/android/app/AlarmManager" rel="nofollow noreferrer">reference</a></p> <p>i recommend use <a href="https://developer.android.com/reference/android/app/job/JobScheduler.html" rel="nofollow noreferrer">JobScheduler</a> for android 21 or <a href="https://github.com/firebase/firebase-jobdispatcher-android" rel="nofollow noreferrer">Firebase JobDispatcher</a> for older devices instead of Alarm Manager</p> <p><strong>update</strong>: Google has released WorkManager as part of JetPack <a href="https://developer.android.com/topic/libraries/architecture/workmanager" rel="nofollow noreferrer">Schedule tasks with WorkManager</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload