nfc-uk
nfc-uk

Reputation: 696

Android Daemon type functionality

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!

Upvotes: 3

Views: 1438

Answers (3)

Siarhei
Siarhei

Reputation: 194

I had to implement almost the same functionality: firing notifications form background.

It's rather simple: start a service and spawn a new thread from within the service.

There are many scenario's where Android platform offers some uot-of-the-box goodies where you do not ecessarily have to start threads yourself.

For example:

  • if your thread should just wait for something you can schedule periodic 'wake up' events with AlarmManager which will take care of running in background
  • or if you need to synchronize data in background with back-end you can use SyncAdapter API which also takes care of running in background.

As CommonsWare just suggested:

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

Upvotes: 0

Gene
Gene

Reputation: 46960

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.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006799

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.

Upvotes: 1

Related Questions