Home > Software design >  Android - Prevent process from being killed
Android - Prevent process from being killed

Time:12-15

I have an app with a ContentProvider that executes code based on calls coming to the custom call() API. Sometimes these executions spin away threads that do stuff that might take a long time to complete, e.g. download a few hundred MB on GPRS, and so I want to make sure my process isn't killed in the meantime.

As I understand it, ContentProviders have no special priority and hence may be killed arbitrarily. However, a foreground service has higher priority, and so I am thinking: Is it enough to just put in a dummy foreground service that does nothing into my app (same process), to prevent it from being killed? Because Android can only kill the entire process, not the ContentProvider's spawned threads alone, right?

Or is there a better way to achieve what I want?

The ContentProvider API is already implemented and seems appropriate for the task at hand, so I'd rather not rewrite everything with some kind of a service interface instead.

CodePudding user response:

Is it enough to just put in a dummy foreground service that does nothing into my app (same process), to prevent it from being killed?

Most likely, yes. Ideally, the Notification that you raise gives the user some control over that background work, such as stopping it.

Because Android can only kill the entire process, not the ContentProvider's spawned threads alone, right?

Correct.

  • Related