Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are some good examples in the android sample code</p> <blockquote> <p>.\android-sdk\samples\android-10\ApiDemos\src\com\example\android\apis\app</p> </blockquote> <p>The ones to check out are:</p> <ul> <li>AlarmController.java</li> <li>OneShotAlarm.java</li> </ul> <p>First of, you need a receiver, something that can listen to your alarm when it is triggered. Add the following to your AndroidManifest.xml file</p> <pre><code>&lt;receiver android:name=".MyAlarmReceiver" /&gt; </code></pre> <p>Then, create the following class</p> <pre><code>public class MyAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Then, to trigger an alarm, use the following (for instance in your main activity):</p> <pre><code>AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, MyAlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); Calendar time = Calendar.getInstance(); time.setTimeInMillis(System.currentTimeMillis()); time.add(Calendar.SECOND, 30); alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent); </code></pre> <p>. </p> <hr> <p>Or, better yet, make a class that handles it all and use it like this</p> <pre><code>Bundle bundle = new Bundle(); // add extras here.. MyAlarm alarm = new MyAlarm(this, bundle, 30); </code></pre> <p>this way, you have it all in one place (don't forget to edit the <code>AndroidManifest.xml</code>)</p> <pre><code>public class MyAlarm extends BroadcastReceiver { private final String REMINDER_BUNDLE = "MyReminderBundle"; // this constructor is called by the alarm manager. public MyAlarm(){ } // you can use this constructor to create the alarm. // Just pass in the main activity as the context, // any extras you'd like to get later when triggered // and the timeout public MyAlarm(Context context, Bundle extras, int timeoutInSeconds){ AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, MyAlarm.class); intent.putExtra(REMINDER_BUNDLE, extras); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar time = Calendar.getInstance(); time.setTimeInMillis(System.currentTimeMillis()); time.add(Calendar.SECOND, timeoutInSeconds); alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent); } @Override public void onReceive(Context context, Intent intent) { // here you can get the extras you passed in when creating the alarm //intent.getBundleExtra(REMINDER_BUNDLE)); Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show(); } } </code></pre>
 

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