views:

168

answers:

2

Hi.

I have a new earthquake notification android app thats in it's initial release version. I have a problem currently. My app has a service running in background, this schedules itself to run every X period. To schedule itself it needs to run atleast once. Currently that happens when you boot the phone. Obviously I do not want user to reboot his phone, once he installs the app.

Also what I do not want is to have my app UI invoke it everytime it runs. Because that would clearly screw up the user scheduling. I need to instead know that either when my UI app runs for first time on a phone, it should be able to run the service so that it can schedule itself and keep running as expected in future.

Alternative way that I could solve it is, that if I know a list of existing alarms to wake a service, as then I would know if an app is already running or not.

If you'd like to see the app source code or run a binary file; Please look at the following github url

I hope I am clear in my intent. Just to give some background on app; It's a notification app for android phone which generates notification when an earthquake happens and also shows the affected area on map with additional functionality. It has a UI app, that shows the list of earthquakes in past. And a service that monitors for new ones.

Thanks in advance.

A: 

Note that calls to startService() are not nesting: no matter how many times you call startService() if this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

Every call to this method will result in a corresponding call to the target service's onStart(Intent, int) method, with the intent given here.

You could easily have a boolean flag in your service something like: isScheduled which you can check against to schedule or not your repeating tasks when you receive the event.

And finally if you need a service that runs all the time, you should start it in the Application class, no matter how many times. You do the difference in the onStart method of the service.

Pentium10
+1  A: 

I'm not at home, so this will be brief until I can edit to fill.

First off, I think you can set up a BroadcastReceiver to notify you on package installation for the initial setup.

To detect if you already filed an alarm, try to retrieve the PendingIntent for the alarm with FLAG_NO_CREATE. If you get a null, you need to set up the alarm, otherwise it is probably good.

Walter Mundt
Perfect. I should have thought of it myself. PendingIntent.getBroadcast was what I wanted!! :)
Priyank