Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent); </code></pre> <p>Alarm manager will fire immidiatly if <code>initTime &lt; System.currentTimeMillis()</code></p> <p><a href="http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29" rel="nofollow">from docs</a>:</p> <blockquote> <p>If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval. </p> </blockquote> <p>According to the code you've provided, <code>gcal.getTimeInMillis()</code> will return millisecods corresponding to <strong>Today 9.00</strong>. So if you'll try to run your code after this time, <strong>initTime</strong> will be less then current system time which triggers immidiate run of AlarmManager.</p> <p>To fix this, for example, you can add one day to your calendar before passing its <code>gcal.getTimeInMillis()</code> if it is already in past so it will point to <strong>Tomorrow 9.00</strong> to let it run tomorrows morning</p> <p><strong>Update1</strong></p> <p>Tried code like this and it worked as expected for me - fired service every 10 seconds:</p> <pre><code> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initManager(); } private void initManager() { AlarmManager manager; PendingIntent pendingIntent; Intent intent = new Intent(this, NotifyService.class); manager=(AlarmManager)getSystemService(ALARM_SERVICE); pendingIntent=PendingIntent.getService(this, 0, intent, 0); long initTime = System.currentTimeMillis() + 5000; manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 10*1000, pendingIntent); } </code></pre> <p>However you can use another option: there is <code>AlarmManager.set()</code> method You can fire it like this</p> <pre><code>long runTime = /* your calendar time + 24 hours in millis*/ manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent); </code></pre> <p>so it will fire your service at <code>runTime</code>. And then in the service invoke the same method with recalculated <code>runTime</code> for another day so it could look something like that:</p> <pre><code>public MainActivity extends Activity{ protected onCreate(Bundle bundle){ .... initManager(); } private initManager(){ ... long runTime = /* time of your first alarm/notification/whatever you develope */ Intent intent = new Intent(this, NotifyService.class); pendingIntent=PendingIntent.getService(this, 0, intent, 0); manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent); } } </code></pre> <p>And the service:</p> <pre><code>public NotifyService extends Service{ ... public int onStartCommand(...){ ... /* do stuff */ Intent intent = new Intent(getApplicationContext(), NotifyService.class); pendingIntent=PendingIntent.getService(this, 0, intent, 0); long runTime = /* recalculate it according to the next time of firing this service*/ manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent); } </code></pre> <p>}</p> <p>So your service will register an intent in AlarmManager itself everytime the service fires.</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