问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12807577/is-it-possible-to-have-a-alarmmanager-inside-a-service-class