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
30
votes
1 answer

Is it correct to bind a ViewModel to a Service?

I've started using Architecture Components in my application and I'm still learning how to use it. In my app I have an Activity showing different Fragments sequentially. In some of them I need to communicate with a background service in order to…
30
votes
3 answers

How to stop Android service when app is closed

I'm trying to keep service running continously until user close app. I'm using startService() method from onCreate() method of my main activity and stopService() in onDestroy() method. Now I have problem, because it seems that my main activity dies…
Kamil
  • 13,363
  • 24
  • 88
  • 183
29
votes
3 answers

How to call suspend function from Service Android?

How to provide scope or how to call suspend function from Service Android? Usually, activity or viewmodel provides us the scope, from where we can launch suspend but there is no similar thing in Service
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78
29
votes
4 answers

Android Activity with no GUI

I have created a activity that is only meant to be launched from a link (using a intent filter.) I do not want this activity to have a GUI - I just want it to start a service and put a notification in the bar. I have tried to put the intent filter…
29
votes
2 answers

BroadcastReceiver trying to return result during a non-ordered broadcast - PACKAGE_ADDED in Android

I am getting this exception in my below given code. i don't have any idea what is wrong with this code. Please help me out to get rid of this exception. 05-23 23:33:49.853: E/BroadcastReceiver(26895): BroadcastReceiver trying to return result during…
29
votes
6 answers

How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?

I'm currently creating a foreground service with a notification that appears in the notification bar when the service starts. If the service stops, the notification dismisses. My question is, is there a way to stop the service when "clear all…
d.moncada
  • 16,900
  • 5
  • 53
  • 82
28
votes
5 answers

When to use and when not to use a Service in Android

I have been developing for Android for little less then 2 years, and I am still puzzled by this seemingly simple question. When should one implement a service? From my experience there are some rare cases but I am questioning this because on every…
Igor Čordaš
  • 5,785
  • 4
  • 42
  • 54
28
votes
3 answers

How to monitor SIM state change

I'd like to be able to do some stuff when the SIM state change, i.e. play a sound when SIM PIN is required, but I think there are no Broadcast events that can be intercepted by a broadcast receiver for this... registering for…
27
votes
1 answer

When does ServiceConnection.onServiceDisconnected() get called?

I'm messing with Android services, and I have found that ServiceConnection.onServiceConnected() gets called fairly predictably when I bind to a service. However, my onServiceDisconnected() method seems to never be called, even after the VM dies. I…
sehugg
  • 3,615
  • 5
  • 43
  • 60
27
votes
3 answers

How to use PendingIntent to communicate from a Service to a client/Activity?

I have been reading the following text on the Android Developers Site, specifically under the Framework Topics -> Services -> Starting a Service. There it states the following : If the service does not also provide binding, the intent delivered…
TiGer
  • 5,879
  • 6
  • 35
  • 36
27
votes
1 answer

Observe LiveData from foreground service

I have a repository which holds the LiveData object and is used by both activity and a foreground service through a ViewModel. When I start observing from the activity everything works as expected. However observing from the service doesn't trigger…
Ilya Sosis
  • 352
  • 1
  • 4
  • 9
27
votes
2 answers

IntentService's onHandleIntent(Intent) gets null argument

I'm using an IntentService to run a background service for my app on android. Oddly I'm getting a lot of crash reports with cases where the intent passed to onHandleIntent is null. I'm not even sure how this is possible and seems extremely odd. Can…
27
votes
2 answers

android app that accesses the sim toolkit

I want to build an application that accesses information from the sim toolkit. My carrier allows you to access services like checking your account balance, sending and receiving cash and a whole lot of services. Are there any resources, tutorials or…
gilokimu
  • 531
  • 4
  • 10
26
votes
3 answers

How to run an android app in background?

This code will run an app automatically after booting the system, but the app will close after pressing the back button. If the app is run normally by clicking it's icon. It will continuously run even after pressing the back button or running other…
JBMagallanes
  • 271
  • 1
  • 3
  • 4
26
votes
4 answers

Displaying popup windows while running in the background?

I am trying to open an activity from a class that extends Service. I am performing this task when the app is not in foreground/not being used. I can see in the logs that my service class triggered start activity with Intent.FLAG_ACTIVITY_NEW_TASK…
Sidharth MA
  • 241
  • 1
  • 3
  • 9