Android app How to delay your Service start on phone boot

大城市里の小女人 提交于 2019-12-22 06:57:17

问题


hi When my app get the ACTION_BOOT_COMPLETED it starts a service. I would like to delay that for lets say 60sec. Can i do that in the:

public class StartAtBootServiceReceiver extends BroadcastReceiver 
{

        public void onReceive(Context context, Intent intent) 
        {
           // Delay...60sec
        }
}

回答1:


use Timer() and TimerTask():

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //run your service
            }
        }, 60000);



回答2:


When you receive the BOOT_COMPLETED intent you should use the AlarmManager to setup an pending intent that will fire after 60 seconds.



来源:https://stackoverflow.com/questions/4562269/android-app-how-to-delay-your-service-start-on-phone-boot

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