Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><strong>Service class automatically started after rebooting one time.</strong></p> </blockquote> <p>There are lots of questions posted by many users on this issue "Service automatically started after rebooting".</p> <p>As a solution many of them suggested on return value onStartCommand() method with START_NOT_STICKY (It may worked for many, but isn't for me).</p> <p>Note: it works good for lower versions of android 2.3.3 and 3.0 but not on 4.0 and later versions.</p> <p>After analyzing bit ,came to known that startID seems to be giving different value when it is starting automatically, and on invoking by users.</p> <p>So for me startID played a trick</p> <pre><code>@Override public int onStartCommand(Intent intent, int flags, int startId) { if(startId!=2) { //to do } return super.onStartCommand(intent, flags, startId); } </code></pre> <blockquote> <p>And For tracking your time even after reboot.</p> </blockquote> <p>I am not sure if android provide a API for tracking the time,but it can be achieved with simple mathematical computation.</p> <p>For this you need to track at what time Alarm has fired for last time (use sharedpreference storing date/time in milli seconds).</p> <pre><code>long lastInvoked = preferences.getLong(AndroidAlarmSMS.LASTALARAMINVOKED, -1); Log.d("last_time_invoked",""+lastInvoked); long currentTime = System.currentTimeMillis(); long period = 1000*60*5; // using 5 min of interval to repeat long diff = currentTime - lastInvoked; Log.d("difference",""+diff); if(diff &gt; period) { long result= diff % period; long nextInvokeAt; if(result &gt; period) { nextInvokeAt = currentTime + (result - result); } else{ nextInvokeAt = currentTime + (period - result); } Log.d("invoked_next_time",""+nextInvokeAt); setPendingIntent(context,nextInvokeAt,period); } else { long result= period-diff; long nextInvokeAt = currentTime + result; Log.d("next_time_invoked_else",""+nextInvokeAt); setPendingIntent(context,nextInvokeAt,period); } </code></pre> <p>and do the above operations in your Broadcast receiver class for action "BOOT_COMPLETED" and set the alarm with the resultant time</p> <pre><code>public static void setPendingIntent(Context ctx,long time,long period){ AlarmManager alarmManager= (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(ctx, MyAlarmService.class); PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, myIntent, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, period , pendingIntent); Toast.makeText(ctx, "repeating --&gt; "+time, Toast.LENGTH_LONG).show(); } </code></pre> <p><strong>Note:</strong> It is not a standard answer to resolve this issue, but for time being it work for me, hope it may help you to resolve this.</p> <p><strong>Note:</strong> Looking for better answers too...</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