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
146
votes
4 answers

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 looks like this: Log.d("Refresh",…
maysi
  • 5,457
  • 12
  • 34
  • 62
144
votes
11 answers

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, myService.class); context.startService(i); And then in…
JDS
  • 16,388
  • 47
  • 161
  • 224
143
votes
8 answers

What is the difference between an IntentService and a Service?

Can you please help me understand what the difference between an IntentService and a Service is?
michael
  • 106,540
  • 116
  • 246
  • 346
123
votes
2 answers

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: when I have to implement a service which method is…
GVillani82
  • 17,196
  • 30
  • 105
  • 172
116
votes
14 answers

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 application in the background. How my background…
dhaiwat
  • 1,731
  • 4
  • 16
  • 16
115
votes
6 answers

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); startService(intent); or below: Calendar cal =…
user3629316
  • 1,245
  • 2
  • 10
  • 8
109
votes
16 answers

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 examples? Are there any alternatives? My app service is…
106
votes
2 answers

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 service is restarted by the system the…
rf43
  • 4,385
  • 3
  • 23
  • 28
104
votes
4 answers

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 without a main Activity? Can I launch my app…
Meher
  • 2,545
  • 3
  • 26
  • 53
92
votes
2 answers

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 figured the best way to go about this is to have a…
Utopia025
  • 1,181
  • 3
  • 11
  • 21
89
votes
6 answers

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 is the best approach. I did a lot of research,…
83
votes
3 answers

Android Broadcast Receiver vs Service

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 receiver can be registered in code or the manifest and…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
81
votes
3 answers

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 that right? My first tests show that Work is only…
80
votes
8 answers

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() { super.onResume(); startService(new Intent(this,…
Tim Malseed
  • 6,003
  • 6
  • 48
  • 66
79
votes
5 answers

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 location of the device and calls a web service if…
Thomas H
  • 791
  • 1
  • 6
  • 3