Is it possible to have a AlarmManager inside a Service class?

核能气质少年 提交于 2019-12-02 23:49:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!