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

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

Related

Do I need a Service in my android application?

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.

No idea on how to update widget from service

I've been trying to make a widget to my music app.
The app has a service that handles the mediaplayer so that i can "minimize" the app and yet continue listening to music.
I know that i need to update the widget from the service, but all the examples i've found, wants me to start a new service on each update, and that's not what i want.
Any help is appreciated!
Thanks in advance
You can do it without the service too. The service is recommended because if your updation takes too much time, android may close it and give Application Not Responding (ANR) error message. These are the cases like when you send a web-service request. Otherwise, you can use your custom AppWidgetProvider to update your widget (without making a service), using RemoteViews(the same way you do in the service). For more on how to do that refer this

How to make an application run continuously in android?

I am creating an application which sends the location information to the server. But when the back or home button is clicked the application is stopped. Is there a way to make the application run continuously in background and sends data to the server ?
Use a background Service. Services are application components which can run in the background without user interaction. A good tutorial on Service in android is from this.
You probably want to start the service on launch of your Activity, or you can register for system broadcasts like BOOT_COMPLETE to keep your service running from boot up.
However, keeping your service running throughout without user knowledge is a bad idea, and drains battery also. You probably want to wake up the service using AlarmManager, do processing, schedule next restart, and exit.
You need to run a Service class for your own application.
Refer docs for more information.

android correct control of a service

I have a thread (Updater) inside a service (RefreshTasks) that check if there are some updates on the server. Then I have 4 activities that use those data.
But I have a problem in managing this service...
I would like to keep the service active along the whole application, even if the screen goes off. What is the correct practice to manage a service like this? In this moment I start and stop the service every onResume and onPause method of all activities... but this implies that the service will stop when the screen goes off.
Any suggestion?
Thanks AL.
The Service instance continues to run also when your app is paused.
I suggest you to look also this service provided by Google, which is a good solution for push notification in communication between the device and your service:
https://developers.google.com/android/c2dm/
And this is a good tutorial to start with:
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/

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

Categories

Resources