3

What is happening in Android if I have IntentService defined as following:

public class BackgroundService  extends IntentService {
    public BackgroundService() {
        super("BackgroundService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        run();
    }
    private void run() {
        try {
            while(true)
            {
                //Some expensive Internet & SQL querying stuff
                Thread.sleep(1000 * 60 * 60);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

How bad will it drain the battery? I aim for rather fundamental answer (What will happen if I set the sleep to 1 day)?

Vojtech B
  • 2,837
  • 7
  • 31
  • 59
  • 1
    Your service can remain running but without doing any work, without the need to call Thread.sleep, what exactly are you trying to achieve? – Philio Mar 16 '14 at 22:13
  • @Philio Well, as far as the multithreading environment is concerned I suppose that if I put my thread to sleep I give other threads the resources that I do not necessarily need. – Vojtech B Mar 16 '14 at 22:20
  • As @A--C said, just let it exit when it's done processing the intent. – Philio Mar 16 '14 at 22:24
  • I understand that this is an anti pattern to use the service like this. But what should I do if I need to notify user about some time-based events (such as upcoming lessons) and I expect that user is going to install my app and "never" open it again because he is notified by the Android status bar notifications? – Vojtech B Mar 16 '14 at 22:35
  • 1
    @VojtěchBašta You would use `AlarmManager` + accompanying `BroadcastReceiver`(s) for time-based events. Generating a status bar notification takes virtually no time, but if your SQL & internet queries take up more than 10 seconds, you'll probably have to make the `BroadcastReceiver` acquire `Wakelock` and start a `Service` that does the heavy lifting. – A--C Mar 16 '14 at 22:46

1 Answers1

1

You question to so close to this one. He also trying to do some work inside a loop and make the Thread sleep.

From the Geeks answer in the provided question, I can tell you that it depends on the //Some code line and what will you replace it with. You might replace it with an intensive code that use the internet, GPS, Flash, Camera and consume your battery for sure OR you might replace it with nothing and let the Thread sleep which allow the CPU to replace it with any other Thread that needs to perform anything.

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • Question updated. I do some expensive internet and SQL operations. I this is the case, does this change anything? If so why would it? – Vojtech B Mar 16 '14 at 22:31
  • The answer is **YES** it will consume battery charges for sure. You can make sure of this by opening the `settings` of your device and select from the `LeftMenu` Battery to see how your device resources can consume the battery level :) – Sami Eltamawy Mar 16 '14 at 22:37
  • So if I have Sleep for 1 day it will drain the battery even during the sleep time? – Vojtech B Mar 16 '14 at 22:39
  • It depends on how frequently you are repeating the action that drain the battery. If you did you `Internet and SQLite` actions only once a day will not be the same as repeating it 200 times per day – Sami Eltamawy Mar 16 '14 at 22:44