after I press home button ScheduledExecutorService does not run in the background

别来无恙 提交于 2019-12-12 01:34:59

问题


I have created this in onCreate() and after I press home button ; ScheduledExecutorService does not run every 10 mins in the background.Why ?

Where is the mistake.
Please help me out .

Thanks in Advance.

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleWithFixedDelay(new Runnable(){

            public void run() {
//play music to check if it executes every 10 mins

                  mp3.start();
            }

    }, 0, 600, TimeUnit.SECONDS);

回答1:


I think that AlarmManager is more well suited to repeating a task every 10 minutes than Executor is.

I would suggest setting up an AlarmManager and using a BroadCast receiver to "catch" the trigger that it fires, and inside your onReceive() method you can do whatever you like, including start your mp3. I think this way will work better for you, especially if you are intending for you sound to play even if your application is not currently the Active app on the device.



来源:https://stackoverflow.com/questions/14606039/after-i-press-home-button-scheduledexecutorservice-does-not-run-in-the-backgroun

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