简体   繁体   中英

Android service shutting down

This is my first time experimenting with android services, so I am a little bit lost. I am developing an application that requires a service to run in the background at all time. This service is initialized from an onclick event in the main activity. To start the service I use the following code:

Intent Test = new Intent(this, testService.class);
startService(Test);

In the service I basically have two things. In the method onCreate I initialize a timer and every 30min it opens a new thread and checks if the server has any new data. While on the onStart methods I register a Receiver.

After a couple of hours the service is being killed, is it possible that the garbage collector is removing the service? My suspicion is that the way I am initializing the service it is still binded to the activity "main" process. What can I do to make sure that the service keeps running?

Thanks

You need AlarmManager instead of timer for this task.

OS can kill any normal priority or user defined background service when OS required resources, so if you want that your service run always then you can set your background service to foreground or and if you don't want to let your phone on sleep mode, you can use wake lock...by these you can set your service on always on mode..... Thanks

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