I have a website that has full support for mobile devices but since opening a browser window seems too much and some people are obsessed with installing apps, I'm now creating an android app which is essentially just a browser inside of an activity.

I thought, I'm creating it anyway, so I might just go one step further and enable the app to notify the user when there are new events that may concern them such as new notifications in the site itself.

I've never developed an android application nor have I ever worked with java, I'm basically just googling and copy/pasting with little tweaks (I know my ways around programming, just not java), which made me think twice about how I should do this.

Basically my idea is when the activity is closed (onStop) I will start a service that then is going to send an HTTP GET request to my API site.com/notifications/countUnseen which simply returns an integer, and if there are any I'll create a new android notification for the user. When the activity is resumed (onStart) I will terminate the service. I am concerned mainly about the fact that I have to send an HTTP request every, say, 5 minutes and I'm not really sure how to do that. What I found is that I could use ScheduledThreadPoolExecutor suggested in this answer from within the service.

Do you think that what I have planned is a good idea at all? I just need a word of comfort about this, or better yet another suggestion if what I have is garbage, from someone more experienced than me.

  • 1
    You might be more interested in Push Notifications rather than polling the server every 5 minutes. Commented Aug 6, 2015 at 13:26
  • @GregBurghardt could you elaborate? If you are referencing this google cloud thing, it seems too complicated for such a simple task.
    – php_nub_qq
    Commented Aug 6, 2015 at 13:29
  • Well I guess that was my idea. Another SO question with some relevance which provides some alternatives: stackoverflow.com/questions/1378671/… Commented Aug 6, 2015 at 13:32
  • 1
    Also, on tablet or phone, running periodically something consume energy, and your user might be unhappy by having his battery sucked by some inactive application. Commented Aug 12, 2015 at 7:22

2 Answers 2


There is no need to leave as service running to perform regular polling, and doing so may annoy your users, as it will consume memory for no good reason.

You should look at the AlarmManager API. This has three potential advantages:

  • it can be configured to send an Intent that will start your app in the background even if it is not running, meaning you don't need to waste memory
  • it can be configured to wait until the device is next awake after your selected time, which will save power
  • it is able to batch notifications with similar frequency together, meaning that if any of your users have another process that polls periodically the two will happen simultaneously, thus saving power.
  • I think I've done that by accident, however could you please tell me how I can do point 2? I can add my code to the question if necessary.
    – php_nub_qq
    Commented Aug 12, 2015 at 8:17
  • 1
    See the documentation here: developer.android.com/training/scheduling/alarms.html#type - you're looking for the ELAPSED_REALTIME alarm type (without "_WAKE", which would make the alarm manager force the device to wake up). You also want the setInexactRepeating method of AlarmManager (unfortunately it doesn't seem to be possible to link to the documentation due to having parentheses in the URL)
    – Jules
    Commented Aug 12, 2015 at 8:24

Create a runnable and call it with handler using postDelayed() method in your service.

private final Handler handler = new Handler();
private Runnable getResponceAfterInterval = new Runnable() {

        public void run() {

                new RequestTask().execute("URL of your GET request"); //RequestTask() is a AsynchTask requesting and handling response

            } catch (Exception e) {


            handler.postDelayed(this, 5000*60);


And in onStart() method paste these lines.

  • 1
    While this solution works, I don't think it's great for three reasons: it uses a timer that only runs while the device is awake, so those 5 minute intervals are likely to get stretched out to be much longer; it needs a service running so consumes memory four an unnecessary background process; and finally if your service is terminated due to memory pressure it will stop the repeating task until the user manually restarts the app.
    – Jules
    Commented Aug 12, 2015 at 8:36

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