Questions tagged [android-service]

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().

Android Service Class Reference

Tutorials & Examples

6696 questions
78
votes
3 answers

Background service with location listener in android

I am creating a background service that will run in its own process. It should allow me to listen if the device location has changed. I should be able to change criteria like the distance moved before notifying the UI. How can I do the same? I…
Nilanchala
  • 5,891
  • 8
  • 42
  • 72
77
votes
8 answers

How to get the current location latitude and longitude in android

In my application, I get the current location's latitude and longitude when application is open, but not when the application is closed. I am using Service class to get the current location latitude and longitude in my application. Please tell me…
Anil M H
  • 3,332
  • 5
  • 34
  • 51
74
votes
4 answers

Creating an Android Service with Phonegap? (Have phonegap app run even when closed)

I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I…
Jonovono
  • 3,437
  • 5
  • 42
  • 64
72
votes
1 answer

Can I call stopSelf() in Service.onStartCommand?

There are some conditions where my service could be attempted to be started when it should not be. In cases like this is it bad form to call stopSelf() while inside a onStartCommand() method? If so what is the best way to handle such a situation?…
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
71
votes
8 answers

Fatal Android 12: Exception: startForegroundService() not allowed due to mAllowStartForeground false

I noticed one exception (Firebase Crashlytics) for Pixel 5 and Pixel 4a (both on Android 12), no other devices, happened only two times, one time for each device. What does it mean? Android 11 and 12 have the same rules for working with foreground…
user924
  • 8,146
  • 7
  • 57
  • 139
67
votes
8 answers

Android java.lang.IllegalArgumentException: Service not registered

I have a setup that looks something like this: class MyFragment implements SomeEventListener { Application mAppContext; boolean mBound; boolean mDidCallUnbind; MyIBinder mBinder; ServiceConnection mConnection = new…
dcow
  • 7,765
  • 3
  • 45
  • 65
66
votes
4 answers

java.lang.RuntimeException: WakeLock under-locked C2DM_LIB

I have uploaded my application on google play but users have reported the following exception java.lang.RuntimeException: WakeLock under-locked C2DM_LIB. This exception occurs when I try to release the WakeLock. Can anyone tell what could be the…
Rookie
  • 8,660
  • 17
  • 58
  • 91
64
votes
8 answers

Communication between Activity and Service

I am trying to make my own MusicPlayer for android. Where i came to a problem is running some things in background. Main activity manages GUI and up to now all the songs are playing. I wanted to separate GUI and music playing classes. I want to put…
Dejan
  • 3,046
  • 3
  • 28
  • 43
62
votes
2 answers

Is an android service guaranteed to call onDestroy()?

The life cycle diagram of an Activity on an android does not guarantee that onDestroy() would be called, but that the process may be killed and the Activity is removed abruptly. The life cycle diagram of a Service on an android does guarantee that…
user574771
  • 999
  • 2
  • 10
  • 20
62
votes
3 answers

What is the difference between a background and foreground service?

I am currently writing my first Android application and I keep running into references to background and foreground services. Since I intend on using a service in my application I was hoping to get a clarification between the two and how they are…
60
votes
3 answers

When is a started and bound Service destroyed?

I was going through the services documentation in android when I noticed two contradicting points: In the services document it is specified in Managing the Lifecycle of a Service These two paths are not entirely separate. That is, you can bind to…
anz
  • 1,317
  • 2
  • 13
  • 25
59
votes
5 answers

How to execute Async task repeatedly after fixed time intervals

How to make Async task execute repeatedly after some time interval just like Timer...Actually I am developing an application that will download automatically all the latest unread greeting from the server and for that purpose I have to check for…
Waseem
  • 1,392
  • 5
  • 21
  • 30
59
votes
6 answers

Debugging a service

I have written a service with a remote interface and installed it on my PC's Eclipse AVD. I have a client test harness which starts and invokes methods in the service. Initially I had the service installed by a control class and activity, which I…
NickT
  • 23,844
  • 11
  • 78
  • 121
58
votes
9 answers

Taking picture from camera without preview

I am writing an Android 1.5 application which starts just after boot-up. This is a Service and should take a picture without preview. This app will log the light density in some areas whatever. I was able to take a picture but the picture was black.…
eyurdakul
  • 894
  • 2
  • 12
  • 29
58
votes
3 answers

Why does my Android service get restarted when the process is killed, even though I used START_NOT_STICKY?

My app uses a pattern where I start a service with Context#startService() as well as bind to it with Context#bindService(). This is so that I can control the lifetime of the service independently from whether any clients are currently bound to it. …
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118