Kamil
Kamil

Reputation: 13931

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.

Upvotes: 0

Views: 66

Answers (3)

Sney19
Sney19

Reputation: 123

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

Upvotes: 3

Rostislav Pashuto
Rostislav Pashuto

Reputation: 196

There is a good article on android.com website https://developer.android.com/training/location/receive-location-updates.html

Upvotes: 0

cafebabe1991
cafebabe1991

Reputation: 5176

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]).

Upvotes: 0

Related Questions