Android Activity has destroy after few minutes when app is pause - android

I create an application which get location and send data to server in 5 min intervals. But when my app is paused after 2/3 min app is destroyed. I want to running service till I destroy activity. Please give some suggestion.

There are so many reasons lets your app crashes when going background. You should give some code.
Your problem, I think your device is Android O or later. In Android O, if you create a BACKGROUND service, the app will be destroyed in the background, you should use FOREGROUND service instead.
Reference: https://developer.android.com/about/versions/oreo/background

If you intend your application to work even when the app is not visible to the user, use a Service, which runs in the background.
Using a basic Service is just as easy as creating an activity, give it a try.
Also, posting relevant code that shows what you've tried could also help.
Since this app runs in the background, be sure to optimize usage of location services so as to save the phone's battery.

Related

Foreground service getting killed by the OS

I'm creating an app that captures location every 30 seconds. To do so, I've a foreground service and a handler in it that gets the latest location every 30 seconds. The app is working just fine for many devices with stock OS. But, on some devices like OnePlus, Panasonic, Vivo etc. the foreground service gets killed by the OS(sometimes the app too) even after changing the battery optimization status and the doze mode. I know that it is not possible to create a service in Android that does not die. Is there any way I can achieve what I'm trying to? Please let me know. Thanks!
First of all, yes you are right the operating system will stop services when resources are limited, so to get around this you state the type of the services while creating them as the following:
START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT...
read the last part of this article it talks about when to use which of them
https://android-developers.googleblog.com/2010/02/service-api-changes-starting-with.html
Service.onStartCommand() callback that allows the service to better control how the system should manage it. The key part here is a new result code returned by the function, telling the system what it should do with the service if its process is killed while it is running.

android: how to make sure my app doesnt get paused when running in the background

Android app that I am working on reads from near by beacons(devices) using bluetooth. It works fine when the app is in the foreground (tested it for 20 minutes). However, few minutes after app goes to background it stops reading.
I notice when app goes to background, onpause() method is executed; still my app reads for few minutes and then simply stops reading anything (when I manually bring the app to foreground, oncreate method is executed and app continuous normally).
Why is my app stopped reading few minutes after it went to background. My app is an activity and not service.
should convert the activity to service or
should I create intentservice or
should I create foregroundserive
I donot understand the difference between above 3 types of services and if any of them would help me.
Though slightly older threads, I reviewed Prevent that the app get stopped or paused by the OS and How can we prevent a Service from being killed by OS? and my app killed by android system when it running in background
But I am lost. Any discussion is appreciated
EDIT
As I understand from #davidgyoung answer, I have to write a service. I assume GUI portion of my app goes into mainactivity; then how I can ensure my mainactivity/GUI is still active in memory and was not killed by Android by the time service tries to broadcast/notify GUI
/EDIT
An Activity is not designed to run for long periods in the background. The Android OS will destroy activities that are not visible as memory is needed for other functions. While a Service is the proper alternative, even a service will be destroyed under memory pressure by the OS, so you still need to restart the service if it is killed by the OS and you continue to want to do beacon scanning.
All of these issues came up when we built the Android Beacon Library, and we settled on these solutions to keep scanning going:
Use a Service to scan for beacons in the background. It does not have to be an IntentService, but that is a reasonable option.
Use an AlarmManager to restart the scanning service 5 minutes in the future in case it gets killed. (This delay allows the OS to time to recover from a temporary need for extra memory.) If the scanning service is still running, just reschedule the alarm.
Register for OS level events (boot, power connect/disconnect) to restart the scanning service at a later time if the user kills the app with the task switcher.
All of this is built for you if you decide to use the Android Beacon Library (and we welcome contributions, too!) If you want to roll your own, you may want to look at the source code to see how these things were built. Feel free to copy and modify, too. That's the beauty of open source!
Full disclosure: I am the lead developer on the Android Beacon Library open source project.

prevent app to clear application data while running in background

I am developing an android application related to gps. that is sending location information periodically to the server. sometime i am getting process killing issue in some devices.
if my application running in foreground then its working file. but while we minimize app and move it to back or once phone going to sleep then app automatically clearing all application data and variables. and then when we tried to resume activity back, it showing blank information and generating exception.
how to prevent clear app data while app running in backgtound? i want to keep this information as it is. this issue only arise in some devices not all.
i have also tried research on Google, but not getting any good solution.
Please Help me. Thank you.
You can't do it like that. If you want your application to keep sending data to a remote service even if the app is in the background, you need to do it a long running Service. Keep in mind that a service runs by default in the main thread, so you need to run your comms with the server in a background thread (however you like it, asynctask, wtv). Then, to really make sure Android doesn't kill your service if it's running low on memory, you need to set your service as a foreground service with a notification.
That way, even if your app is sent to the background you're sure Android the communication will continue.

Execute code on location changes if app not running

I'm writing a small android app that need to execute some code when the devise change location.
I was wondering if it is possible to execute code on location change even if the app is not running (not background but stopped).
I mean, is there some system services that "wake" (or launch) my app (or a specific activity of my app) when lat/lng changes?
Until now I've create a locationManager, set some criteria, a provider and set the requestLocationUpdates. This is working but if the app is not running I got no update.
You need to create a Service that is notified of the updates. Services run in the "background" even when your app is not running.
This can get complicated since when a user is in your app, if you expect the app to update, then the activity will need to bind to the service.
Here is a tutorial:
http://www.vogella.com/tutorials/AndroidServices/article.html
And the official docs are good too:
http://developer.android.com/reference/android/app/Service.html
Be careful - services can be shut down by Android when memory runs low. Users can also "force stop" your app. Don't assume that it is ALWAYS running, but you can assume it runs most of the time.
And pay attention to the Service Lifecycle - it's different from and Activity:
http://developer.android.com/guide/components/services.html#Lifecycle

Android:nonstop Back ground service

I have started a service from my application and from that service a worker thread is started .I want my service to run even application goes background and until the user kills/exits the application.
But some cases my service got killed due to low memory ,then used sticky service or making the app to foreground to restart the service.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method, but in this case how we can control that thread.
Please let me know is it the right approach ,and is this usecase achievable
I want my service to run even application goes background and until the user kills/exits the application.
This is not possible. The user can always get rid of your app, via Force Close in Settings, or via some device's version of the recent-tasks list.
But some cases my service got killed due to low memory
No, your process is terminated for low memory.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method
No, because your process is being terminated.
Please let me know is it the right approach
Probably not. Very few apps need a service that runs constantly, which is why Android, and its users, go to great lengths to control such services. I would recommend that you try to find some solution to whatever your problem is that does not need a service running constantly.

Categories

Resources