开发者

Android Service restarts randomly

开发者 https://www.devze.com 2023-01-19 11:17 出处:网络
I\'m trying to write a Service which sits and waits for SMS messages (using a Broad开发者_如何学GocastReceiver which the server registers) and will take some action depending on the details of the SMS

I'm trying to write a Service which sits and waits for SMS messages (using a Broad开发者_如何学GocastReceiver which the server registers) and will take some action depending on the details of the SMS message. I use an Activity to startService() and stopService() for the Service. After I close the Activity, the Service continues to sit there with its state kept and its BroadcastReceiver waiting, as it should... However, I find that over time, randomly, the Service will restart itself. That is, onCreate() and onStartCommand() will be called even when the Service has already been started. This happens sometimes when my phone sits idle overnight...

Can anyone provide insight on why this is (phone requests resources and kills a service?), what I can do to prevent it, or what I can do to prevent the state of the service from being lost?

Thanks in advance.


I don't think you need to start service from activity at all. Have a broadcast receiver that listens to your SMS messages and starts the service. After work is done, service should call stopSelf();


I'll try Service.startForeground()


Android sometimes (when it's low on resources, mem/cpu) removes running services and once it's high on resources again brings them back. This is why you see that your services has been restarted a couple of times during night.

0

精彩评论

暂无评论...
验证码 换一张
取 消