A
A
Alexey Mukhin2019-01-27 23:07:05
Android
Alexey Mukhin, 2019-01-27 23:07:05

How to implement ContentProvider in Service?

In android studio I created a service and an application - two different projects that work on the same device. The service inserts records into its database table once a minute, which then need to be queried in the application.
I want to share the Service database using ContentProvider. Is it possible? (since both the Service and the ContentProvider have their own well-defined structure and set of methods)
If you make two applications, one without an Activity in it's ContentProvider, the second with an Activity in it, we turn to the provider of the first - everything is clear, everything works.
In my case, the Service and the Application, by analogy, I try to contact the Service provider - the cursor is empty.
Created a provider class in the Service with all its methods and constants:

public class MyContactsProvider extends ContentProvider ....

Registered it in the manifesto.
then in the onStartCommand method I do this:
public int onStartCommand(Intent intent, int flags, int startId) {
        ContentProvider myContactsProvider = new MyContactsProvider();
        myContactsProvider.onCreate();
        return START_REDELIVER_INTENT;
    }

I can’t understand what and how else should be registered in the Service in order for its provider to work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Varakosov, 2019-01-28
@login-l

You don't need to initialize a provider to access it. Check the description in the manifest, the permissions, and the URI you are accessing in the ContentResolver or loader.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question