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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • show us what you've done so far so we can provide guidance. – Jonathan May 02 '15 at 04: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 – Monika Patel May 02 '15 at 04:39
  • edit your question to put the code in the question, not the comments. – tumultous_rooster May 02 '15 at 04:47
  • 1
    This sounds like the most battery inefficient setup ever. What are you doing that needs to run every 5 seconds? – ianhanniballake May 02 '15 at 04: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 – Monika Patel May 02 '15 at 04:54
  • Run every 5 seconds and battery dead after 5 minutes. Did you try IntentService that relauch itself berofe get killed? – Deividi Cavarzan May 02 '15 at 05:06

3 Answers3

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

JoeAdeoye
  • 170
  • 2
  • 10
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)

Kirankumar Zinzuvadia
  • 1,249
  • 10
  • 17
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

Ilja KO
  • 1,272
  • 12
  • 26