Always Running Android Application
Solution 1:
In my Android app, I need to get data from a web service each x seconds in background (application must seem to not be running).
May I use Android Services? http://developer.android.com/reference/android/app/Service.html
Yes, you may use a Service but the question is do you want to poll a web service every x seconds? You will drain the battery VERY fast that way. What you could do is either schedule an inexact repeating task (it will be called APPROXIMATELY in 15 minutes intervals but will be bunched with other apps to lower battery usage). See AlarmManager.setInexactRepeating(). Or use push notifications using Google Cloud Messaging (GCM) to let your server notify and wake up your phone when there is new data.
When the data from the web service is the one I am expecting, I do need to show an Android Notification. How can then load my Application in a concrete activity?
You can do both from the Service. A Service is a Context, so you can show Notifications, start Activities and such...
How can I ensure the background "service" will keep running even if it crashes? This is, how do I restart that background part or "service" of my application?
You can schedule a repeating Service start Intent in the AlarmManager. If your Service is running, it should be ignored, if not - it will start your Service.
To ensure your Service is not killed by the system easily (when low on memory for example), use Service.startForeground() and be sure to return START_STICKY from Service.onStartCommand().
Solution 2:
May I use Android Services?
Yes. Use IntentService
I need to get data from a web service each x seconds in background
Use AlarmManager
When the data from the web service is the one I am expecting,
I do need to show an Android Notification.
Use BroadcastReceiver and Notifications
How can then load my Application in a concrete activity?
Use Intent to launch activity from notification
How can I ensure the background "service" will keep running even if it crashes?
This is, how do I restart that background part or "service" of my application?
You need to fix what is causing the crash. Else everytime the service runs, it may cause crash. AlarmManager will restart your service at regular intervals.
Solution 3:
*emphasized text*Pls find the below answers.
May I use Android Services? http://developer.android.com/reference/android/app/Service.html
Yes. You have to use service.
When the data from the web service is the one I am expecting, I do need to show an Android Notification.
When you service finds the desired data from the webservice show a notification
How can then load my Application in a concrete activity?
User clicks the notification to launch the activity
How can I ensure the background "service" will keep running even if it crashes? This is, how do I restart that background part or "service" of my application?
Set a periodic alarm to start and stop the service, this way you dont drain the battery.
Solution 4:
Yes you are right.. use services. on getting the data make the service to start an activity of the app.
Regarding issues with services crash.. Write another global service that will keep a check if the data fetching service is running. The moment it finds the service has stopped. Make the monitoring service to restart the data fetching service back again.
Post a Comment for "Always Running Android Application"