Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Daemon type functionality

Tags:

android

If I spawn a thread via AsyncTask from the UI thread, is this thread killed when the UI thread terminates?

My AsyncTask (spawned fom UI) performs operations and then calls the Notification Manager as appropriate (part of my applications functionality). This works well, but notifications cease when the application exits, and I am assuming this is because the UI thread has terminated, therefore so do the children.

I did consider a service (assuming initially it would perform similar to a daemon) but then read that these run on the UI/main thread so would not be persistent across the UI thread termination.

My question really is how can I get the functionality of a daemon spawned from an Android app? I don't need permissions outside the spawning parent process, and it doesn't need to be persistent across reboots.

POSIX API'ish threads through the NDK or am I completely wrong?

Only spent a couple of days with Android so still trying to feel my way around. Many thanks!

like image 571
nfc-uk Avatar asked Feb 10 '26 00:02

nfc-uk


2 Answers

Threads execute within a process. Android suspends (for later reuse) or kills an application's process when it's destroyed, which takes all threads with it. So the daemon would have to be a disconnected process, not a thread. Android is deliberately set up to prevent you from starting these (though sub-processes are straightforward with Runtime.exec() and its relatives). I think you can do what you want by fork/exec()'ing in the NDK, but the phone will have to be rooted to run the resulting app, which creates many problems. Not least is that warranty is often voided for a rooted phone.

like image 148
Gene Avatar answered Feb 12 '26 14:02

Gene


If I spawn a thread via AsyncTask from the UI thread, is this thread killed when the UI thread terminates?

Not automatically and not immediately. The thread will run to completion, or until Android terminates the process, whichever comes first.

I did consider a service (assuming initially it would perform similar to a daemon) but then read that these run on the UI/main thread so would not be persistent across the UI thread termination.

Services are not really a "daemon" in classic Linux sense. A service is automatically in the background from a UI standpoint. It is not automatically in the background from a threading standpoint. Any work the service does that will take some time should be done on a background thread. The difference is that with a service running, Android will not be as prone to terminate your process quite as quickly.

My question really is how can I get the functionality of a daemon spawned from an Android app?

That depends on what features of a "daemon" you are trying to obtain, which you neglected to describe in your question.

POSIX API'ish threads through the NDK

That will do you no good. Your threads will still be terminated when your process terminates.

like image 40
CommonsWare Avatar answered Feb 12 '26 15:02

CommonsWare