android – Which thread runs ContentProvider?-ThrowExceptions

Exception or error:

If I call to a ContentProvider from a Activity, which thread is ContentProvider running in?

E.g. What happens if the Activity is killed and a query is executing in the ContentProvider? Say that you have a slow network query f.ex.

How to solve:

If you mean the normal use case of using a ContentResolver to call a ContentProvider then here is what happens according to the best of my knowledge:

  1. I am assuming in this example that your ContentProvider lives in one process and your Activity in another process.

  2. If the ContentProvider has not been created then the onCreate() method is called using the “main” thread of the application’s process.

  3. The query()/insert()/update()/delete() methods are called using BinderThreads that sit around in every application process waiting for incoming commands from other processes.

So what happens if the Activity that triggered the query()/insert()/etc is killed in the middle of one of these method calls? I can’t say for certain but I’m fairly confident that whatever is happening in the ContentProvider will continue because the ContentProvider process should go on unaffected but what happens after that method returns I can’t say for certain.

I’m not sure how a slow network would be involved in this at all unless your content provider is backed by a network instead of the usual sqlite db?


There are two possibilities when invoking a ContentProvider function (query/insert/update/delete/etc):

  1. Your ContentProvider is in the same process as the caller. If so the ContentProvider function runs synchronously on the same thread as the caller.

  2. Your ContentProvider is in a different process as the caller. If so the ContentProvider function runs on a binder thread in the ContentProvider process.

In both cases the the caller is blocked until the ContentProvider function returns. As always read the full documentation from Google and/or the AOSP source code.


I stumbled upon this while troubleshooting an issue where a remote process (other than UI) in the same application would continually spawn the content provider each time it ran a query. Unfortunately unless you write something custom, ContentProviders are never destroyed as long as a process is running. The content provider normally runs in the UI process thread (which was not open), so in this case of a remote process querying the contentprovider, it would spawn a new instance of the contentprovider upon each query because that separate process (UI thread) was not running at the time.

Perhaps there is a better way of doing this, but instead of spending a ton of effort rolling my own db or content provider to close the db and resources on every create, I discovered a simple workaround.

It turns out has the same ability to run in a separate process as a service does. So in my example I simply moved the content provider to the same process where the remote service was located. Problem resolved, now the content provider runs in the same process and only spawns once as long as the service is running. Also check out the android:multiprocess attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *