Do I need a Service in my android application? - android

In my Application I have several Activities and a Java class with a thread. This thread continuously receives and sends messages to the web server. The Application is working fine in testing but I am worried that it may not when being used in actual phones. I am afraid that my background thread which continuously interacts with the Server might be killed by Android, while the app is in background.
Do I really need to make my threaded client class a service ? I want to start it when user signs in and stop it when the user logs off from the system.
If I need to do it please suggest some easy tutorial for it.

See these links..
http://www.vogella.com/articles/AndroidServices/article.html
Example: Communication between Activity and Service using Messaging
http://androidtrainningcenter.blogspot.in/2012/04/simple-example-of-service-in-android.html

As far as i think you better use TimerTask along with Timer along with any normal class or service will suffice your objective.

Related

Android Services with Intent Services real time examples

I am getting confused between service and Intend service,what is the difference between service and Intend service,then on which kinds of situations we have to use service,Intend service any can explain with some real time example?
Thanks in Advance
A Service is a piece of code that will run on your main UI thread and will remain running until stopped, even if you aren't in the foreground.
An IntentService is a special kind of Service that starts its own Thread and queues incoming start calls to run on that thread one at a time, in the order they came in.
Use Service if you need a place for long running actions to occur that need to continue even if the Activity is killed. For example, tracking location via GPS for a maps app. Use IntentService for repetative pieces of work. For example, downloading files. Or syncing a database. You can think of IntentService as kind of like an AsyncTask that runs in a Service.

Do I need service to do something when app is in background?

I have to write an app that is supposed to send user location to server.
I have class that is listening events from Android and it sends data to server bia webservice.
Do I have to put that in some service, or I can just put it into my MainActivity class code?
Im asking, because Android is still weird for me. It pauses activities and I don't know if this will work "from the background" or not.
Of course I want to update location when application is in foreground and background too.
Yes you will have to use a Service to send data in the background as well as foreground.
You can use android Service or Background Service,
based on your requirements I would suggest using the Background Service.
Here are the links for both:
Service:
http://developer.android.com/guide/components/services.html
Background Service:
https://developer.android.com/training/run-background-service/create-service.html
if you want the location irrespective of the fact that the application is open or not then you need to have a service which will keep on posting your location to the server.
Here's how you can do it ...
Read this answer
Note :
Activities can't do what you want to achieve as they exist till the user exits them (by pressing the back button, killing them or clearing the stack[Need not think of it for now if a newbie]).
There is a good article on android.com website
https://developer.android.com/training/location/receive-location-updates.html

Background Service in Android - Need Help

I need some help or suggestions regarding Background services.
Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on running.
In my views there is a Button whenever I press that button, that button passes some files to the Background Service and my Background service upload that file onto some server.
I am done with the uploading process. Now I want to know that how can I make a Background Service that always keeps on running and on my tapping of the button it sends a file to the Service.
I am new in Background service implementation.
Please guide Friends with some tutorials, suggestions or guidelines.
Thanks a bunch
You've probably already read some of the Android Service documentation, but I suggest studying it further and looking at the Local Service Sample if you have not done so already:
http://developer.android.com/reference/android/app/Service.html
It sounds like you have already got your Service up and running, and I think the actual problem you are trying to solve now is how to communicate data from your Activity to your Service. When your Activity is bound with a Service that's part of the same application, that service is in the same process and runs on the same main UI thread, so once you get the IBinder object from the Service after binding with it, you can simply directly call the functions in that Service from your Activity. Similarly, you can pass your Service a handler object from your Activity so that the service can send messages or post Runnables to your Activity. Communication with a local Service is therefore quite simple.
So if you take a look at the Local Service Sample in the link above, you will see a section in the code where we get a reference to the Service once binding has completed:
mBoundService = ((LocalService.LocalBinder)service).getService();
After that point, it's possible to directly call methods on that Service that's in the same application. For example, you could have a method called sendFile in your Service. In your Activity, you might do something like:
mBoundService.sendFile( myStuffObject );
There are quite a number of questions on Stack Overflow regarding communicating between an Activity and a Service, and I think you'd find it beneficial to search and read these.
A standard Android service will do just fine in this case.
It will continue running in the background untill its work is finished or until you ask it to stop.
There is a topic on the android dev site explaining services in detail.
you should go for android Service that is used for Background operation . Inside the service your have use TimerTask which will be checking the Queue for every x sec and when any items present in the Queue it will pull the item and upload it to the server.
here is the link for Android Service..
http://developer.android.com/reference/android/app/Service.html
Link fro Queue.http://developer.android.com/reference/java/util/Queue.html

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.

Android - Run in background - Service vs. standard java class

In Android, if I want to do some background work, what is the difference between
Creating a Service to do the work, and having the Activity start the Service
VS.
Creating a standard java class to do the work, and having the Activity create an object of the class and invoke methods, to do the work in separate thread.
Doing your own threads is overkill, there are solutions for this, so you don't have to worry about the hard parts of concurrency. Have a look at AsyncTask or IntentService. If you go for a service please keep in mind that your service can be killed at any time.
Well, Android provides some useful methods for making worker threads easily. Look at the Looper class definition. It allows you to send events via a Handler to be executed one after another in another thread or transmit messages between different threads.
A service is nothing fancy. Creating a Service is just a way of telling the OS that you need to do some work even when your Activity is not visible.
Depending on the application you're building it might not be an option.
Nearly every network application will have some of its functionality on a Service to allow the user change active Activity while something is being downloaded.
In an RSS reader, for example you can click 'Update all' and, depending on the current data connection, it might take a bit longer than you wish. So if you want the user to be able to get back to the Home screen and do anything else while the files are being dowloaded you'll have to use a Service.
A Service allows you to run tasks on the background while the user is not on your Activity. This doesn't mean it'll be running all the time. Check the Service lifecycle.
BTW IntentService is a service.

Categories

Resources