Android Foreground Service Crashed - android

I have been facing issues with Android service and their restart behavior. At present i have a Service (SERVICE 1) and a thread which is used to move files from one folder to the other. This works great.
But when I am in my app and start the moving service, and i swipe the app away from recent tasks(systemui.RecentTasks) screen the service is stopped with a log "Scheduling restart of crashed service". This service is a foreground service with an active notification.
I do have another service (SERVICE 2) which is also a foreground service with notification. This service also creates a thread to run a background operation. But when I swipe the app this service is not killed and there is not log from ActivityManager.
The final scenario is when the SERVICE 1 stopped frequently from recent tasks within small intervals , again and again, the SERVICE 2 is also stopped now with the same log. I can understand this, as the system detects frequent crash from this app, it tries give less priority to it even if it has foreground service.
My question is why this strange behavior. Both are running in foreground and with an active notification. why does the SERVICE 1 Is stopped and SERVICE 2 is not? I don't find enough resources of service restart behavior when it is crashed. Also i don't find information as how the android system manages to schedule a restart. FYI SERVICE 1 returns START_REDELIVER_INTENT and SERVICE 2 returns START_STICKY.
Kindly help me with this issue. Please. Thanks

Related

Android killing my foreground service 1 minute after the screen gets turned off

I'm developing a player app.
For this reason, it uses a foreground service to handle the playback.
Until recently the service was bound to my activities.
This is not the case anymore.
Since then, some specific devices (mostly Pixel 1/2/3) have been killing my app 1 minute after the screen has been turned off
The service is a foreground service not bound to anything.
Why would the device kill it?
As soon as the app is excluded from the device-optimized apps list the issue is solved
I'm not providing code, because I'm just trying to understand if this situation makes sense and if so what should I do to prevent this
BTW the app is using a receiver to act on Screen_ON/OFF messages. That's how I can see in the logs that the player service onDestroy() method gets killed exactly 1 minute after the screen has been turned off
what should I do to prevent this?
The key point here to keep the service alive is as said in official documentation :
While an app is in the foreground, it can create and run both
foreground and background services freely.
so, we can conclude that keeping the work in foreground and visible to the user has very minimal chances of being killed. And to do so we need to know that how android gets the idea that this process is in foreground ?
Here are the criteria's at which a process is said to be in foreground:
It has a visible activity, whether the activity is started or
paused.
It has a foreground service.
Another foreground app is connected to the app, either by binding to
one of its services or by making use of one of its content
providers. For example, the app is in the foreground if another app
binds to its:
-IME Wallpaper service
-Notification listener
-Voice or text service
If none of those conditions is true, the app is considered to be in
the background.
If none of the above criteria is fulfilled by your app process then thats the reason of your service being killed.
You can read more on this topic here :
Foreground service being killed by Android

Keep Android service running even after Activity is closed

I am building one of those SOS apps. Whenever the device is shaken above a threshold value (detected through accelerometer), I am showing a Toast (as of now)
1) App is launched. User gives name, email, etc.. and clicks finish on last screen.
2) Service is started which keeps listening for shake.
3) It detects the shake correctly if the App is running.
4) If I close the app (the activity), the service gets killed along with it.
How do I keep the service running even if app is closed, so that it can listen to shakes from background? (That's the whole purpose of this app)
[1.I am returning START_STICKY in onStartCommand
I also tried using a BroadcasterReciever which will restart service by receiving broadcast from onTaskRemoved
I am testing on ASUS Xenfone Max, Marshmallow OS
]
You have two options:
Start your service as foreground service (with startForeground(int id, Notification notification): docs. But in this case you will have to show Notification in notification tray for as long as your service is running
Use separate process for your service adding in manifest to your process android:process=":nameofyourprocess"
Try starting a service without binding it to the activity (Simple unbound service). Return null on your onBind() function. Sticky services attach itself to a activity and has a lifetime as long as the attachment survives. You might have a constant notification related to your application when you use foreground services.
You can put a Service in foreground, in which case it will always be considered as active (and it will therefore have its own notification, so the user knows that an active Service is running). It won't be stopped until it goes back to background. That is what you want in your case, as you want your Service to stay alive as long as possible. As described in the Android Service documentation:
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.
The idea is the same for Activities and Services, actually: when Android needs memory, it starts killing processes. The foreground processes (e.g. the Activity that is displayed on the screen, or foreground services) have a higher priority than the ones that are in background (say, a paused Activity), so they will be the last ones to be stopped by the system.
Using START_STICKY just tells the system that if it has to kill your Service, then you'd like it to restart it can. That doesn't say this Service is higher priority than the others.

Android: Service is stopped when clearing ram storage

I have coded a simple app in Android Studio. What it does is not important but it starts a Service with Context.startService(Intent i). Till now everything is allright BUT when I kill all tasks with my Task Manager of my phone the Service is killed, too and with it the notification ist creates! I don't understand why.
By the way: I used return START_STICKY at the end of my Service. And it has been started/sheduled by the Timer class with Timer.scheduleAtFixedRate(TimerTask task, int delay, int period). The Service has been started by my Main Application or by the BroadcastReceiver which received BOOT_COMPLETED. The timer has also not been canceled.
Hope you will pardon my english.
Services that are started with startService does not stop until an explicit call - stopService is done on that particular service. Another case when the service stops is when the phone needs more memory its stops the background services and applications. In your case, you are doing the exact same thing. Clicking on the kill all tasks clears the memory which is the exact thing that happens when phone is out of memory and wants more memory. In that case - services are stopped and sticky services are restarted. Now, how to handle the restart of the service properly, kindly search clearing memory stops services android and you will be shown a bunch of answers on stackoverflow !

Keep a Service started at BOOT running even if Application stopped

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService

how to start a service properly and keep it alive?

I know there are other question with the same topic, but I didn't find an answer to my questions.
my goal is to have a service which works on the background as a location listener, and it won't be stopped when the application is stopped (either by a task killer).
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
now, the first problem is that my application crashes but the service has started, and when I run the application again it works.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
and it won't be stopped when the application is stopped (either by a task killer).
Fortunately, this is not possible. If your user wishes your service to stop, the user can stop the service via a task killer or the Manage Services screen in Settings.
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
Usually, you only use one or the other, not both.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
No, because you called startService() in addition to bindService().
The service stops when the application is closed by the task manager. If this could not be possible every app would have its own service running without any user control over them. You could start the service at boot up and then when the user uses task manager to close, you could restart the service.

Categories

Resources