Background service to update UI continuously - android

I would like to run a service that gets data from an external sensor and updates the UI every second without blocking the main thread. What is the most efficient possibility to make it? Thread, IntentService, Service? Or anyting else?
Thanks in advance!

You should use a binded service if you want to update UI regularly and the activity has to access the service method.
Service vs IntentService
Check how binded service in android works in this link

I think you should use binding service.Read here.
Here are some examples:
example1.
example2

First off- Services run on the UI thread. So a service won't fix the problem of blocking the main thread. Of course getting a sensor reading is quick, so you shouldn't need to worry about it.
What else are you using the data for? If its only to update the UI, no need for a service. Just register for sensor events in your activity and update the UI when you get a sensor event. If you want to do something else, like save the data even when the app is in the background, then you may want to look at Services.

JobScheduler should be used if your app targets Android 5.0 (API level 21) or later.

Related

When to use headless activity and service in Android?

I am working on an android device where configuration change is not going to happen. In this case I have few units of work that I want to delegate to a particular Android entity. The work does not involve UI. Should I delegate this non-UI work to a service OR a headless activity?
In other words, what are the benefits of headless activity as compared to a service?
Thanks.
Consider WorkManager to schedule your tasks.
Otherwise, I would suggest you use a foreground service.
I'd use a service. It runs in the background, user can do other things, until the service finishes.

Android NonUI Thread making application "not respond"

I am editing the code of an android app that is making GPS calls in a service. LocationListener. It also uses ServiceConnection
In some views the device decides that my application is taking too long to respond, and that the user can either "Force Close" or "Wait". Before this popup appears, the application is still usable by the user, they can scroll, slide, press buttons etc.
I am only assuming this is related to the GPS service as it is running whenever this problem happens.
I heard that this problem has to do with a thread running on the UIthread, instead of a background thread. But I was sure that services run asynchronously in the background thread.
Insight appreciated
Using a service does not necessarily spawn a new thread, the service call runs on it's caller thread. From the android API Service doc at:
"Note that services, like other application objects, run in the main thread of their hosting process..".
You can specify the service to run on a different process but best practice is to spawn a new thread in the service.
More on android service at:
http://developer.android.com/reference/android/app/Service.html
I fully recommend you to extend AsyncTask,it enables proper and easy use of the UI thread. Allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. You may want to do all the computing in doInBackground method.BTW Force Close or Wait is a classic behavior for this kind of issues.
Good Luck!!!
Processing in a service can still cause your application to hang.
The solution you should be looking at implementing is to run any logic that may bog down your activity in a separate thread. This includes things like: Database updates/insertions, Network communication, and any other pieces of long running code.
The AsyncTask is a convenient method for this as you can manipulate the UI in the onPreExecute and the onPostExecute functions.
You can implement an AsyncTask directly in your service as a subclass.
Hoepfully this helps!
Cheers

service doesn't keep constant control of main thread on UI

I have a service in my Android application that has a UI its running. The problem I am running into, it that on various devices, it doesn't seem to maintain control of the main thread. For instance, it does not ALWAYS respond to the back button being pressed. Is there a way to ensure that the service always has control of the UI?
Thanks.
Solved it! For anyone that cares, I need to called the startForeground method for the Service.

whether I should go for a service or handlers? in android

I am creating a app that would fetch data from a website every 2 minutes ..
I need to know whether I can go for a service ?
I tried using handlers and threads but it stops after 3 or 4 hrs I already tried using Async task but it's not working out..
You have to use service because its always runs in background . Thread and AsyncTask and any other handlers not give guarantees to runs in background life time of applicatin.
Here you can find good tutorial for service.
http://marakana.com/forums/android/examples/60.html
http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-for_04.html
http://mylifewithandroid.blogspot.com/2008/02/double-life-of-service.html
u use the service good option.because service is always running background process
if want know about the service
http://developer.android.com/reference/android/app/Service.html
Best to use a service, actually the best option there is to it. Always consider the users of your application as they do not want to see all that incoming and outgoing flow of data.

What to use: service or threads

I am developing an android app which fetches/uploads data from/to the web service every n minutes. This upload/download is only done when the app is running. But this might change in future.
I dont update the UI when the new data is downloaded. The UI is only updated if the user is on the current screen(app have multiple activities)
My question is what is the best approach to this problem.
I dont think service is the right approach as it sounds like an overkill(in the present scenario). AlarmManager could be an option.
Running threads inside a service be an option ..something like this .
Any pointers/suggestions would be great.
Thanks
I am using AsyncTask in my activity to ask .net web service some information and it works and easy to use.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Well, in this case, since the app would already be running during the time, either would work great, but a service can be called from anywhere within the application so this is where I would use the service over the thread.
If you want to create the thread to only be used in lets say Main.java, then thread would work fine, these are the only things that I can see really making ANY difference at all, they're really pretty close, and in this case neither gives a distinct "correct" answer, but I would choose Service
I think all approaches you noted would work ok. Personally I'd go with this:
Use AlarmManager to wake download service. Start it when Activity is shown, stop it when activity hidden.
Download service should be short lived: start it to do the upload/download and then shut it down.
If download service does get some new data, it sends a Broadcast which Activity listens to.
Just broadcast a message after your upload/download is done, and then have a receiver start the service and then have that service stop itself. And you are done.
This should be done if you dont plan on polling the server for new information or anything. Primarily this kind of approach would be for onetime update, interpret, finish. And wait until the next update. Which primarily for most cases is streaming.. but depends on what you are getting.

Categories

Resources