简体   繁体   中英

Android : Run Service Continuously

Hello Techies , I am facing a problem in achieving some task for my personal application. I want to make a service to run continuously once it is started and should not stop even if the screen gets locked or phone goes to sleep mode! I made the service to return as sticky but even then the service runs until my screen is not locked or it doesn't go to sleep mode! Thanks in advance for the help! :)

I think you can try timer task it will help you

TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();

public void doWifiScan(){

scanTask = new TimerTask() {
        public void run() {
                handler.post(new Runnable() {
                        public void run() {
                         wifiManager.scan(context); 
                         Log.d("TIMER", "Timer set off");
                        }
               });
        }};


    t.schedule(scanTask, 300, 30000); 

 }

please refer to this answer here as it describes how to repeat your service as much as you want using the AlarmManager.

and please give me feedback

Hope that Helps .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM