Question

I'm wondering if there is any way, to run a background thread, that works behind all activities. I need to renew session on my PHP server in my mobile app, but I don't want to make it in doInBackground of every AsyncTask class, but in some thread that works behind all activities and connects to server every 15 minutes, to renew the session, so I can store SID in database, and use it every time I need in my requests.

I read about Handler and Service, but as I understood, they work only in a specific activity, not in the background of the entire application, and I can't renew session with them as I need. Am I wrong?

Or can it be handled somehow with RoboSpice lib? I heard about it, but never checked its features.

Sorry if I missed similar question already asked and answered.

Était-ce utile?

La solution

RoboSpice is designed to make it easy to write network requests that are executed in background. So, yes, it can help to extend the session on the server side from the client side.

Nevertheless, RoboSpice has nothing to do with a timer. So, to achieve this, you will need :

  • to use the alarm manager of Android
  • then give it a pending intent to wake up a service of your own
  • this service can use RoboSpice to create a network request that will extend the server's session.

Details here : http://developer.android.com/training/scheduling/alarms.html

Don't forget to unregister properly from the alarm manager when you don't need your session extension anymore or you will drain battery.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top