4

I need to call a background service every 5 seconds, but I am facing a problem in Android 5.1 (Lollipop): it automatically considers the interval time 1 minute.

Please help me to run a background service every 5 sec.

6
  • show us what you've done so far so we can provide guidance.
    – Jonathan
    May 2, 2015 at 4:22
  • Intent i = new Intent(this, BLEAutoSync.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pi); In above code "INTERVAL_TIMER" is 5000 ms. but in android 5.1 its automatically take a minute. so my service is calling after every 1 minute. and in android other version it's work perfectly, It's run my service after every 5 sec May 2, 2015 at 4:39
  • edit your question to put the code in the question, not the comments. May 2, 2015 at 4:47
  • 1
    This sounds like the most battery inefficient setup ever. What are you doing that needs to run every 5 seconds? May 2, 2015 at 4:48
  • Sorry Matt O' Brien. Next time will take care of it ianhanniballake : i want to scan ble in background. and auto sync time will be anything. its depend on user. right now one user try to scan ble in background after every 5 seconds. its not working in android 5.1 May 2, 2015 at 4:54

3 Answers 3

6

Since the minimum interval for alarms is one minutes on Android 5.1, You can tweak your service to repeat job every 5 seconds:

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                startJob();
            }
        });
        t.start();
        return START_STICKY;
    }
    private void startJob(){
        //do job here

        //job completed. Rest for 5 second before doing another one
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //do job again
        startJob();
    }
}

The above code ensures one job is completed before it executes another job, hence its a cleaner approach. But if your requirement does not need to ensure completion of one job before executing another job, you can use the code below:

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    startJob();
                    Thread.sleep(5000);
                }

            }
        });
        t.start();
        return START_STICKY;
    }
    private void startJob(){
        //do job here
    }
}

And finally, you can start service by calling startService(); from your activity thread. By this, you are bypassing AlarmManager restriction. Hope it helps...

4

In Android 5.1, they appear to have set a limit, where low polling periods are rounded up to 60000ms (one minute). Repeat intervals higher than 60000ms are left alone.

For timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

So if you want to achieve your goal you have to use handler to do that in android 5.1.

Refer this link https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)

0

I don't see a problem

Either you let the service run all the time and let it wait 5 seconds b4 new tasks are being processed

Or you start every minute a thread which does every 5 seconds the tasks you want...and comes to a halt after a minute maybe just kill, initialize new one it and start it right away

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.