Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ContentProvider with Services

I have a ContentProvider subclass doing all my database work, and a Service that is running in the background.

I can't seem to be able to bind a Service to the ContentProvider. Can I call a method from the service within the ContentProvider at all, or is there a way to bind?

like image 388
Carrie Hall Avatar asked Dec 07 '12 15:12

Carrie Hall


People also ask

Are content providers still used in Android?

A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.

What is Android ContentProvider?

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications.

What is ContentProvider and ContentResolver default ContentProvider of Android?

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

Why does the content provider need to be declared in the Android manifest?

A content provider is a subclass of ContentProvider that supplies structured access to data managed by the application. All content providers in your application must be defined in a <provider> element in the manifest file; otherwise, the system is unaware of them and doesn't run them.


1 Answers

I binded the service in the onCreate() method

@Override
    public boolean onCreate() {

        mContext = getContext();

        .. do other things

        doBindService();

        return true;
    }

and then used the context to actually bind the service

void doBindService() {

        mContext.bindService(new Intent(mContext,
                SomeService.class), mConnection, Context.BIND_AUTO_CREATE);
    }

I don't unbind from the service, but it seems to work ok

like image 61
Carrie Hall Avatar answered Oct 12 '22 12:10

Carrie Hall