Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Set AlarmManager like this:</p> <pre><code>private static final int REPEAT_TIME_IN_SECONDS = 60; //repeat every 60 seconds AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), REPEAT_TIME_IN_SECONDS * 1000, pendingIntent); </code></pre> <p>Change <code>AlarmManager.RTC</code> to <code>AlarmManager.RTC_WAKEUP</code> if u want to wake up phone when it goes off. More about AlarmManager <a href="http://developer.android.com/reference/android/app/AlarmManager.html">here</a></p> <p>Those two parameters also means that your alarm time will be <a href="http://developer.android.com/reference/java/lang/System.html#currentTimeMillis%28%29"><code>System.currentTimeMilis()</code></a> which is time in UTC.</p> <p><strong>EDIT :</strong></p> <p>Your solution using <code>AlarmManager.ELAPSED_REALTIME</code> which measure time since device boot including sleep. It means that if you want run this code after 10 seconds and then want to repeat it and your device is running for more than that, <strong>PendingIntent will be triggered immediately because 10 seconds after boot occurs in the past.</strong> </p> <p><strong>EDIT 2 :</strong></p> <p>If u want to run code just once after 10 seconds try this:</p> <pre><code>private static final int START_AFTER_SECONDS = 10; ... if (radioBtnChecked) { Runnable mRunnable; Handler mHandler = new Handler(); mRunnable = new Runnable() { @Override public void run() { Intent serviceIntent = new Intent(MyActivity.this, MyService.class); MyActivity.this.startService(serviceIntent); } }; mHandler.postDelayed(mRunnable, START_AFTER_SECONDS * 1000); } </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