-1

For example my app, it starts and runs the service(which is meant to check the database for new messages) but as far as I know you can have a AlarmManager to keep opening the Service if so is not opened.

But can you have aAlarmManager inside the Service class to keep on checking for new message in the database? Instead of re-opening the Service class just keep checking the database?

I want to know if it is possible to have a AlarmManager inside the Service class to keep checking the database every X seconds, because the Service will open when the App is Launched

The real question: Is it possible to have a AlarmManager inside the Service class that runs a Method() to check a database?

baTimá
  • 554
  • 1
  • 10
  • 28
  • You can create a `Service` that check's whether there are new messages in database and that same service can be scheduled periodically using `AlarmManager` class. – Vishal Vyas Oct 09 '12 at 20:09
  • 1
    You *want* to be "re-opening the Service class just keep checking the database". Or, more accurately, your *users want* you to be "re-opening the Service class just keep checking the database". Users get irritated with developers who keep services around, where those services are spending most of their time twiddling their digital thumbs, waiting for time to pass. – CommonsWare Oct 09 '12 at 20:10
  • I want to know if it is possible to have a AlarmManager inside the Service class to keep checking the database every X seconds, because the Service will open when the App is Launched. – baTimá Oct 09 '12 at 20:11
  • What do you mean by "have an AlarmManager inside Service"? Do you want to schedule alarms from your service? Or do you just want to check your database from service repeatedly? – Nikolai Samteladze Oct 10 '12 at 21:02

1 Answers1

0

Use a Timer

    Timer myTimer = new Timer();
    myTimer.schedule(new TimerTask() {          
        @Override
        public void run() {
                 /**
                  *
                  *Do something. CODE HERE
                  */
        }           
    }, 0, 30000); //30000 = 30 Seconds Interval.
baTimá
  • 554
  • 1
  • 10
  • 28