Issue
I am implementing app that is going to be always running and connected to server.
So the tablet has nothing to do other than running this app, and checking the server all the time for updates to show on the screen.
What can be the best practice to keep the app always running and connected?
In my experience, i have 2 options to solve this problem:
- Service always running and connected to my activity to keep it updated.
- Keep the work in threads within the activity, since the activity will stay always on. The app will be always on.
My questions are:
- What is the best practice for keeping the app running all?
- What is the best practice to keep the app connected ? Threads within activity? or service connected to activity?
- Is there any preferable structure for such application type?
thank you
Solution
- Battery is something that i will always take into consideration especially when networking task has to be done. So analyze the point where the is absolute necessary to connect to server.
- Whenever you server needs to contact the device use GCM push notification that will save your battery juice which you will spend in polling the server
Well for
Service v/s Thread
is concern the use service in which you should spawn worker threads which will perfrom the networking task these threads will notify the main service thread when they are done for that you need to user Handlers in android
i will favor service over thread because if you close the activity network request will still be fulfilled once request is complete save it in some storage form(database/share pref/content provider) and retrieve it from them. Once you are done dont for get to destroy the service by all stopSelf a appropriate point or else the service will keep exhausting you phone resource. which will make you app a bad citizen.
A simple implementation of the about pattern i mentioned is IntentService which will automatically spawn the worker thread i.e you just have to override handleIntent method and do all the heavily lifting there which will not be on main thread. And IntentService kills it self when if finds itself doing nothings The question which you have asked to my understanding is related to syncing with server you can find more about in from android docs http://developer.android.com/training/building-connectivity.html
Answered By - Munawwar Hussain Shelia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.