After rebooting my device,sending sms frequently is sending sms one more time also while switch on

早过忘川 提交于 2019-12-05 19:57:24

Service class automatically started after rebooting one time.

There are lots of questions posted by many users on this issue "Service automatically started after rebooting".

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).

Note: it works good for lower versions of android 2.3.3 and 3.0 but not on 4.0 and later versions.

After analyzing bit ,came to known that startID seems to be giving different value when it is starting automatically, and on invoking by users.

So for me startID played a trick

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
 if(startId!=2)
        {
               //to do
            }
 return super.onStartCommand(intent, flags, startId);
}

And For tracking your time even after reboot.

I am not sure if android provide a API for tracking the time,but it can be achieved with simple mathematical computation.

For this you need to track at what time Alarm has fired for last time (use sharedpreference storing date/time in milli seconds).

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 > period)
            {
                long result= diff % period;
                long nextInvokeAt;
                if(result > 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);
            }

and do the above operations in your Broadcast receiver class for action "BOOT_COMPLETED" and set the alarm with the resultant time

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 --> "+time, Toast.LENGTH_LONG).show();
    }

Note: 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.

Note: Looking for better answers too...

Hi Karthi just have a look at this code:

Start your service exactly after 5 minutes when the device is booted

Calendar cal = Calendar.getInstance(); 
  // Start 5 minutes after boot completed
  cal.add(Calendar.MINUTE, 5); 
  alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), REPEAT_TIME, pendingIntent);

Here alarm will not trigger before this cal.getTimeInMillis() time

Also go through this void android.app.AlarmManager.setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

Just Remove this permission from manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

and also remove 

<intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"> </action>
    </intent-filter>

from <receiver> Tag in manifest.xml 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!