Reputation: 265
I'm developing an application that is meant to get information about the users location and the current weather. The information is gathered in this way: StartGathering --> Get lat/long --> Get location data (city, address etc) from coords --> Get weather info --> Save information in SQL database. Everything is done by sending a string array using bundle between the different activities containing the data. For this to be accurate the process is repeated every X minute, but since I've worked with activities the updating process interferes with other applications on the device. I've added android:theme="@android:style/Theme.NoDisplay" for each of the activity in the process, thinking this would be the same as doing it in the background. I want the information to be gathered and saved in the database in the background, and as far as I know this isn't possible without using services.
Is there a way to easily convert a bunch of different activities to services, and thus allowing the app to gather the necessary information in the background so that the user can use the device without any performance loss? Or is it possible for activities to do some work in the background without interfering with other applications?
Upvotes: 2
Views: 5084
Reputation: 46856
thus allowing the app to gather the necessary information in the background so that the user can use the device without any performance loss
If your program is doing anything then you are causing "performance loss", perhaps not significant or noticable(to the user), but loss nonetheless. It seems to me that what you are trying to do (get location and make a web request to get weather info related to location every X minutes) is going to cause pretty significant drain on the battery depending on what interval time you are using. I suggest you leave the interval time up to your users if you do not want them to complain about your app draining their battery.
Upvotes: 2
Reputation: 48871
Is there a way to easily convert a bunch of different activities to services...
No, create a Service
(or Services
) and copy/paste your existing code in. Be aware though that a Service
runs on the application's main thread (aka UI thread) so if there is any time-consuming work to be done you'll need to use worker threads or perhaps use IntentService
which does it's work by default on a background/worker thread.
Or is it possible for activities to do some work in the background without interfering with other applications?
No, not in the way you're attempting to do it. Think of an Activity
as being analogous to a Windows Form (or similar). In other words it is a framework for a UI allowing the user to interact with the app. There are ways of doing background work in an Activity
(using AsyncTask
for example) but an Activity
isn't a generic class which can be hidden away whilst it does some 'number crunching'.
Upvotes: 3