Android Application dies after couple of hours - android

I made an android application with a runnable that checks something ever minute.
But the problem is the application goes [DEAD] after a couple hours without an error messages or anything.
Anybody have any idea what the problem could be?

That is not a problem, that is actually expected behaviour.
The lifecycle of all apps is managed by the Android OS. It decides whether to terminate an app in order to free resources and keep the system responsive. Apps that are currently is use have priority over paused and background ones. I think in your case the OS just decides to shut down the app because it thinks it's not needed anymore.
There are ways to get around this, but it all depends on what your app actually does. I don't know your implementation details, but may want to look into sticky Services or the JobScheduler in order to achieve what you want. Keep in mind that there is no such thing as a perpetually running background task that comes out of the box in Android (not should there be one) and usually implementations have certain limitations.

It could be the Android OS itself closing the app. If the OS requires more memory it will start to kill of other processes that have not been used for a long time (i.e. interacted with). You haven't stated how the check happens but it shouldn't be done directly within the app, but it sounds like it is something that a background service should be doing the work which would likely prevent this from happening.

You should NOT use Runnables for background processes, as they get suspended/killed by the OS as soon as your app goes off-screen.
If you want to let some processes run regularly, you have to stick with AlarmManager / BroadcastReceiver combination

Related

How to have a service to run eternally on Android?

Search engines and Android developer website didn't help and I guess you can help with my problem.
I want to make an app for personal use, which is supposed to run all the time on my old tablet (powered all the time). The app will have several features requiring user interaction but independent of those, it should run a background job to check something continuously (real time!) for instance sound detection. It should also always try to connect another device on the network.
That means that job needs to run almost eternally without being killed. Some comments I have found suggested AlarmManager or BroadcastReceiver. But those are triggered by very defined triggers (either time or broadcast). I don't want that, because it should perform its task continuously all the time. This background job should also be able to communicate with the main Activity of my app to report what it is doing and allow user to interact with it (change settings of the job for instance).
Do you know any way how to accomplish this? Is IntentService correct choice for this (hoping that it won't get killed or maybe I should let the Activity to restart it?)
Thanks!
Do you know any way how to accomplish this?
Build your own custom ROM, with a modified version of Android that contains your code as a native Linux daemon.
Otherwise, what you want is technically impossible.
You can come fairly close by using a foreground Service (not an IntentService) and returning START_STICKY or START_REDELIVER_INTENT from onStartCommand(). Android may terminate your process from time to time, but it should restart your service automatically after a short while. That service can use its own background threads to do whatever it is that you are trying to do.

How to keep thread alive when use close application via swiping

I have a thread in my application's result page. And it's calculating the values every minute.
My problem is when user close application via swiping from recent applications, thread is stopping. But it must work.
I know i could create a service-intentservice for it, but that is my first application and i wrote lots of code about that tread, also that thread changes the values at result activity page. So that will be a togh operation for me.
I looked for it so much but i couldnt find an answer. Can you please give me an advise if i can keep that thread alive easily?
Thanks
Put your thread in a foreground Service
Normally, the entire VM is killed when a user swipes your app out of recents. But in recent versions of Android, foreground services are not killed. (Thanks to Steve M for bringing this change to my attention.) On older versions of Android, even foreground services were killed. This was considered a bug. Unfortunately, Google is notorious for inappropriately closing issues as "obsolete," so there's no way to know when it was actually fixed except by testing different devices. (Or even whether it was deliberately fixed at all, or accidentally fixed as a result of some other change.) I'm fairly certain that foreground services were killed in Android 4.4. They are not killed in Android 5.1, at least on my devices, including one that used to be running 4.4. But note the comments on that bug report about foreground services being killed in 5.1 and 6.0. It may be that this was never truly fixed, and it's still hit or miss.
Note that regardless of whether the VM is killed, the foreground service's onTaskRemoved(...) will always be called. (At least in my testing.)
You can test whether the VM is being killed by putting this code in an activity:
private static final long vmInitTime = System.currentTimeMillis();
// in onCreate(...)
((TextView) findViewById(R.id.timeView)).setText(new Date(vmInitTime).toString());
The displayed time will not change as long as the same VM is running. (Or to be more precise, as long as the same definition of your activity class exists. In practice, the ClassLoader has the same lifetime as the VM.)
You could run the thread in a foreground service, if it makes sense to be displaying a notification to the user. In this case the service will continue to you run on many devices, as long as the device is not force-stopping the app.

Can I have an application always open on Android?

I don't know much about Android multitasking but I thought I'd ask the question before I attempt my project.
Is there a way I can program an Android application (aimed at Android 4.0+ only) to always be open in the background and keep all the network connections alive and the UI "drawn" so that when I open it, it ALWAYS opens instantly and I can use it instantly even if the tablet is doing something else?
Thanks.
Most likely, you want to create a foreground service. A foreground service is a service that the user is aware of and is not considered a candidate to kill if Android is running out of memory. It is associated to a persistent notification bar, that the user can tap to bring to the foreground an activity. To make sure that the network connection are not switched off, your service should acquire a wake lock.
However, please remember that a long running process that potentially kills the battery is considered a bad practice, and you should avoid doing this unless you have really really strong reasons to do it.
No, Android is not build like that. The OS can always kill services/activities in the background when it needs more resources.
You can make services which are always running (sticky service) which restarts if it is killed because of resource problems, when there are resources again available.
With Activies you cannot do that. But it could be happen that your activity is 'paused' and still in the background, so it can be 'resumed' very quickly. But again Android can easily kill it for resources.
There is no way you can have an application always running in the background, unless you modify Android at the firmware level and build your own version. Android kills other apps as and when it needs more resources to run the app currently in the foreground.
However, it is possible to make your app better at handling this by saving data and it's current state in onPause() and restoring the same in onResume().

Can I keep Android App alive in background?

I currently have programmed a normal Andorid-App (no Service). It contains a timer for periodic checks. To avoid hassle with a service and communication between Service and App I ask myself whether there is a way to keep an App that is no longer in foreground alive.
Currently when I "close" the App, it is still alive until Android OS decides to kill it. Is there a way to avoid this kill - e.g. by a certain command in "onDestroy" or a certain App-flag?
My App is quite complex and I do not want to implement a Service as this -especially the communication/binding- increases the complexity. Is there an "easy way" or am I really forced to use Service+App? Maybe there is a trick to register the App for sth. special that has the side-effect that Android OS does not kill it when it is in the background.
Edit for better understanding: It is ok that the GUI can go into the background (vanish) when the user wants it, so my question is not how to let the GUI of my App permanently in the foreground. All I want is that the timer stays intact without the need for an additional Service.
Thank you all for ideas in advance!
Android is very unpredictable by the nature of the OS's killing selection and by the market fragmentation. I would not count 100% on anything being kept alive if it is crucial. However you can gamble and be pretty successful. This is what would help you:
Use very good "Best Practices" to keep your string pool and heap at a minimum as Android looks to kill memory hogs first off (and because you love what you do).
Add the persistent attribute to your application manifest tag.
To really help yourself out, run as a, or run a service because they are long running processes, are very light (if implemented well), and Android looks to kill these off lastly.
Give your service priority by running it as a foreground service.
Doing these things will increase the likely-hood that Android will not kill your application.
I don't think there is anything like that available. I suggest looking at AlarmManager for periodic tasks - this may mean you won't need to use a service.
The apps being available in the background is simply a caching measure by the android OS to avoid having to relaunch a frequenly used app from scratch. If you want to be able to count on your app running in the background, a Service is the correct solution. Its not the answer you are looking for, but I am not aware of any tricks to staying alive in the cache, and if there were any, I would not feel good about recommending them.

Why dont Android applications provide an "Exit" option?

Is there something in the Android developer guidelines that disuades developers from providing the option to "exit" (stop running) an application from within the application itself?
I love multitasking and all but it's not clear to me why:
the vast majority of apps don't have their own Exit functions and hence just keep running forever
don't give you a choice about running when you turn on the phone - they just do by default
Both of these things lead to memory usage constantly increasing and your device running with this performance burden all of the time despite the fact that you may only want certain apps to run some of the time.
Am I missing something?
Is there something in the Android
developer guidelines that disuadea
developers from providing the option
to "exit" (stop running) an
application from within the
application itself?
Yes. It is generally not needed, just as it is generally not needed to restart a Web server because some user with a browser decided (s)he is done with a Web app.
the vast majority of apps don't have
their own Exit functions and hence
just keep running forever
They don't keep running forever. Android will close things up as needed.
don't give you a choice about running
when you turn on the phone - they just
do by default
Those developers aren't paying attention to me.
Both of these things lead to memory
usage constantly increasing
Generally, it doesn't. If you find specific apps that do this, uninstall them.
and your device running with this
performance burden all of the time
Generally, it doesn't. If you find specific apps that do this, uninstall them.
Also, this question is a duplicate of this one.
"Both of these things lead to memory usage constantly increasing"
Which doesn't matter since Android apps are limited to a fixed amount of RAM. Freeing RAM won't give more RAM to other apps.
Essentially, there's no need for a quit button as long as the developer does a good job of designing their app. Android activities are stopped when they aren't visible and resources are needed elsewhere, so the are no longer consuming resources. You can read about the lifecycle here:
Here's a related question:
From Google's Android Application Fundamentals page:
Shutting down components
A content provider is active only while it's responding to a request from a ContentResolver. And a broadcast receiver is active only while it's responding to a broadcast message. So there's no need to explicitly shut down these components.
Activities, on the other hand, provide the user interface. They're in a long-running conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also remain running for a long time. So Android has methods to shut down activities and services in an orderly way:
An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().
A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().
Components might also be shut down by the system when they are no longer being used or when Android must reclaim memory for more active components. A later section, Component Lifecycles, discusses this possibility and its ramifications in more detail.
So it seems like Content Providers and Broadcast receivers should never be explicitly shut down, as they are inert while not handling their specific events.
As for Activities, I would argue in favor of having an end to it, but in certain cases. If your app has a finite state in which the user is done using it, why keep it alive until GC gets it? The activity manager still needs to keep track of that Activity while the user has finished their task. My best example for this is the Calculator. You open it, you have it solve a problem for you, and then you close it. If the onCreate function is so expensive that it's more effective to do onCreate once and then onRestart whenever the user moseys back to your application then you're probably doing something wrong. Maybe I'm misinterpreting how Android handles Activities, and if so I'm always interested in learning more :)
It all comes back to the answer that users want total control of their running and auto-start list and what they do and don't want installed, example: google maps, etc etc. there are no two ways about this.

Categories

Resources