Alright so let me explain what I’m doing and then I have a couple questions.
Explination
I have an app I’m working on that will use a service to check my webpage for updates every 1-10 minutes. So on ACTION_BOOT_COMPLETED, MyReceiver(Broadcast Receiver) is triggered and if it’s an ACTION_BOOT_COMPLETED broadcast it starts a service called BootService. BootService then sets an AlarmService that triggers MyReceiver, and then BootService is terminated. When an alarm broadcast comes into MyReceiver it triggers another service called UpdateService which will check the internet at whatever interval that is set by BootService.
I have a service called BootService because everytime my app is started, it starts up BootService to make sure the AlarmService is running just in case it gets terminated for whatever reason.
When I make my HTTPClient requests I want it to be login protected so what I was trying to do is whenever BootService is run, it will initiate a HTTPClient request to my website, login and store a session id that I’ll use in UpdateService. However, when I put the HTTPClient request into BootService I get a ANR (Application Not Responding). I tried putting the HTTPClient request into an AsyncTask and still I have issues with it.
Questions
-
Would it be better if I did away with the BootService and instead just have MyReceiver set an AlarmService. And whenever someone loads the app just sent out a broadcast to initiate the alarm, again?
-
What could be causing all the hold up during the HTTPClient request that it locks up my program and forces a ANR? How could I properly implement my HTTPClient request so it wouldn’t hold up my Service?
-
Any good examples of a login/cookie store method. And a service that later on uses a session id to make future requests?
Unless the Web service allows infinite session durations, making this work could be tricky. I suggest that you do the login on each update.
This means that you were trying to do way too much work in
onStartCommand(), oronCreate(), oronDestroy(), oronBind(). All of those methods are called on the main application thread, and so you cannot do significant work there.Yes.
Network I/O can be slow.
Use an
IntentServiceand do your network I/O inonHandleIntent().