I made a simple android service with an infinite loop in a thread that posts a string log and a counter.
How ever, from time to time the thread I created stops (specially if I run other applications on the device) and gets restarted randomly after. Of course, I lose the state of the service (ie the counter resets).
Is that an expected Android behavior or I'm doing something wrong?
public class WebService extends Service {
Thread svc_thread;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(svc_thread == null) {
svc_thread = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(true) {
Log.v("WebService", "Doing loop in service "+i);
i++;
try {
Thread.sleep(1000);
}catch(Exception e) {
Log.e("WebService", "Error ocurred in service thread!" + e.getMessage());
}
}
}
});
svc_thread.setName("Service Thread");
svc_thread.start();
}
return START_STICKY;
};
Is that an expected Android behavior
Yes. Processes do not live forever. You may wish to read the documentation about processes .
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.