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.
6,711
questions
1056
votes
34
answers
602k
views
How to check if a service is running on Android?
How do I check if a background service is running?
I want an Android activity that toggles the state of the service -- it lets me turn it on if it is off and off if it is on.
823
votes
11
answers
344k
views
Service vs IntentService in the Android platform
I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?
I also believe that an IntentService runs in a different thread and a ...
600
votes
10
answers
303k
views
Example: Communication between Activity and Service using Messaging
I couldn't find any examples of how to send messages between an activity and a service, and I have spent far too many hours figuring this out. Here is an example project for others to reference.
This ...
474
votes
18
answers
320k
views
Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent
On application launch, app starts the service that should to do some network task.
After targeting API level 26, my application fails to start service on Android 8.0 on background.
Caused by: java....
449
votes
4
answers
344k
views
getApplication() vs. getApplicationContext()
I couldn't find a satisfying answer to this, so here we go: what's the deal with Activity/Service.getApplication() and Context.getApplicationContext()?
In our application, both return the same object....
344
votes
15
answers
199k
views
Trying to start a service on boot on Android
I've been trying to start a service when a device boots up on android, but I cannot get it to work. I've looked at a number of links online but none of the code works. Am I forgetting something?
...
320
votes
4
answers
220k
views
START_STICKY and START_NOT_STICKY
What is the difference between START_STICKY and START_NOT_STICKY while implementing services in android? Could anyone point out to some standard examples.. ?
318
votes
32
answers
412k
views
Android Fatal signal 11 (SIGSEGV) at 0x636f7d89 (code=1). How can it be tracked down?
I've been reading the other posts on tracking down the reasons for getting a SIGSEGV in an Android app. I plan to scour my app for possible NullPointers related to Canvas use, but my SIGSEGV barfs up ...
312
votes
7
answers
167k
views
Get Context in a Service
Is there any reliable way to get a Context from a Service?
I want to register a broadcast receiver for ACTION_PHONE_STATE_CHANGED but I don't need my app to always get this information, so I don't ...
274
votes
13
answers
176k
views
How to have Android Service communicate with Activity
I'm writing my first Android application and trying to get my head around communication between services and activities. I have a Service that will run in the background and do some gps and time based ...
251
votes
5
answers
123k
views
Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE
Lately we have suddenly been seeing a few of the following stack traces. Why could that be? This is from when the app tries to move an audio commentary service into the foreground with a media ...
179
votes
10
answers
194k
views
Android – Listen For Incoming SMS Messages
I am trying to create an application for monitoring incoming SMS messages, and launch a program via incoming SMS, also it should read the content from the SMS.
Workflow:
SMS sent to Android device
...
173
votes
12
answers
269k
views
Start Activity from Service in Android
Android:
public class LocationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
startActivity(new Intent(...
160
votes
3
answers
67k
views
Exported service does not require permission: what does it mean?
I created a service that is bound by other applications through AIDL, and I add it to the manifest as follows:
<service android:name=".MyService">
<intent-filter>
<action ...
151
votes
6
answers
90k
views
Difference between Service, Async Task & Thread?
What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when?
148
votes
4
answers
50k
views
PendingIntent does not send Intent extras
My MainActicity starts RefreshService with a Intent which has a boolean extra called isNextWeek.
My RefreshService makes a Notification which starts my MainActivity when the user clicks on it.
this ...
145
votes
11
answers
253k
views
Android - implementing startForeground for a service?
So I'm not sure where/how to implement this method to make my service run in the foreground. Currently I start my service by the following in another activity:
Intent i = new Intent(context, ...
143
votes
8
answers
121k
views
What is the difference between an IntentService and a Service? [duplicate]
Can you please help me understand what the difference between an IntentService and a Service is?
126
votes
2
answers
100k
views
Android onCreate or onStartCommand for starting service
Usually when I create an Android service I implement the onCreate method, but in my last project this does not work. I tried implementing onStartCommand, and this seems to work.
The question is: ...
117
votes
6
answers
65k
views
Differences between Intent and PendingIntent
I read through some articles. All seem to do the same thing and I was wondering what is the difference between starting the service as below:
Intent intent = new Intent(this, HelloService.class);
...
117
votes
14
answers
119k
views
Determining the current foreground application from a background task or service
I wish to have one application that runs in the background, which knows when any of the built-in applications (messaging, contacts, etc) is running.
So my questions are:
How I should run my ...
111
votes
16
answers
133k
views
How to startForeground() without showing notification?
I want to create a service and make it run in the foreground.
Most example codes have notifications on it. But I don't want to show any notification. Is that possible?
Can you give me some ...
107
votes
2
answers
29k
views
Reasons that the passed Intent would be NULL in onStartCommand
Is there any other reason that the Intent that is passed to onStartCommand(Intent, int, int) would be NULL besides the system restarting the service via a flag such as START_STICKY?
Also, when the ...
106
votes
4
answers
126k
views
Launch Android application without main Activity and start Service on launching application
I have the following scenario in my application. There is no UI in my application; instead there is a Service which starts on boot up and will continuously run.
How can I configure my manifest file ...
92
votes
2
answers
112k
views
Broadcast Receiver within a Service
I am trying to start up a BroadcastReceiver within a Service. What I am trying to do is have a background running service going that collects incoming text messages, and logs incoming phone calls. I ...
92
votes
6
answers
21k
views
BoundService + LiveData + ViewModel best practice in new Android recommended architecture
I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one ...
87
votes
3
answers
62k
views
Android. Is WorkManager running when app is closed?
I want to schedule nightly database updates. So I use new Android WorkManager. My understanding is that once scheduled it will always run in the background independently from the app's lifecycle.
Is ...
86
votes
8
answers
73k
views
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 ...
84
votes
3
answers
54k
views
Android Broadcast Receiver vs Service [duplicate]
I am trying to clarify the difference between a Broadcast Receiver and Service in android.
I understand that an activity can start a service by calling startService with an intent.
A broadcast ...
80
votes
8
answers
43k
views
Android 9.0: Not allowed to start service: app is in background.. after onResume()
I've got a music player which attempts to start a Service in onResume() of an Activity. I've removed a few lines for clarity, but the code is effectively:
@Override
protected void onResume() {
...
79
votes
5
answers
83k
views
How to always run a service in the background?
I am in the process of creating an app that is similar to the built-in SMS app.
What I need:
a service that is always running in the background
every 5 min. the service checks the current ...
78
votes
3
answers
142k
views
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 ...
77
votes
8
answers
321k
views
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 ...
74
votes
1
answer
12k
views
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 ...
74
votes
4
answers
69k
views
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 ...
68
votes
8
answers
88k
views
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;...
67
votes
4
answers
44k
views
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 ...
67
votes
3
answers
83k
views
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 ...
64
votes
8
answers
108k
views
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 ...
63
votes
2
answers
52k
views
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 ...
60
votes
3
answers
25k
views
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 ...
59
votes
6
answers
50k
views
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 ...
59
votes
5
answers
80k
views
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 ...
59
votes
3
answers
39k
views
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 ...
58
votes
9
answers
81k
views
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 ...
57
votes
6
answers
70k
views
Continue Service even if application is cleared from Recent app
I am having a little issue.
In my application, a Service is started after user is logged in successfully. Previously, the service needed to stop if application was killed. (say, removed from Recent ...
52
votes
6
answers
63k
views
Can I get data from shared preferences inside a service?
I'm developing an android application. I'm using android 2.2
In my application I am capturing GPS data and sending it to service with the 1 hour time interval. If user exits from application it's ...
52
votes
4
answers
35k
views
START_STICKY does not work on Android KitKat
One of my apps has a backgrouod service that uses the START_STICKY return code from onStartCommand to automatically restart when the system kills it.
It seems that this is no longer working on Android ...
51
votes
3
answers
24k
views
Minimal android foreground service killed on high-end phone
I'm trying to create an app that lets users log routes (locations/GPS). To ensure locations are logged even when the screen is off, I have created a foreground service for the location logging. I ...
50
votes
4
answers
32k
views
Injecting a repository into a Service in Android using Hilt
I have an Android project with Hilt dependency injection. I have defined MyApplication and MyModule as follows.
@HiltAndroidApp
class MyApplication : Application()
@Module
@InstallIn(...