Background Service getting killed in android
The main problem is that we cannot say
Services are not meant to be killed. They are meant to run in background as long as we want it to.
Basically, that is not true. System still can terminate the service in low memory and possibly other situations.
There are 2 ways to overcome this:
- If you are implementing the service, override
onStartCommand()
and returnSTART_STICKY
as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal. - If you are not sure 1st approach will work - you'll have to use AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html . That is a system service, which will execute actions when you'll tell, for example periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.
Chinese Android Devices Killing Background Service
This will happen mostly for optimizing battery and improving phone performance,
As you have tried my solution but still you are getting this issue in some devices,
so here is an alternate solution for killing app on swipe from recent app list,
- Ask User to Swipe down the application which will lock application as white list and wont be killed
OR
Add below line for every activity in manifest, which will hide your app from recent app list. hence user cant swipe app from recent apps list.
android:excludeFromRecents="true"
App in background killed after 5 minutes in Android 9
you need to have an foreground service, otherwise the OS will kill you app
Related Topics
Cordova - Error: Failed to Fetch Platform Android
How to Detect App Removed from the Recent List
Wallpaper Not Properly Fit on Device Screen
Handle a View Visibility Change Without Overriding the View
How to Display Fetched JSON Data into Listview Using Baseadapter
How to Set Delay in Android Onclick Function
Android Studio Where to Install Ndk File? (Downloaded It in Zip)
Android Support Multidex Library Implementation
App Getting Stuck with E/Com.Facebook.Internal.Attributionidentifiers
How Are Android Activities Handled with Jetpack Compose and Compose Navigation
Displaying Contact Number and Contact Name in a Custom List View
How to Use Sharedpreferences to Save More Than One Values
How to Find the Android Version Name Programmatically
Cannot Call Getsupportfragmentmanager() from Activity
Android - What Is the Meaning of Stableids
How to Parse the CSV File in Android Application
How to Run (Not Only Install) an Android Application Using .Apk File