Popular Posts

Android Content Provider

Steps to interact with existing Content Providers

Data storage methods like SharedPreferences and file system are most specific to one application. If you want share our data across different applications then we go for ContentProviders. But at content providers can get data from database, file system, xml or network as depicted in the below Image.

contentprovider.jpg

Content providers store and retrieve data and make it accessible to all applications. They’re the only way to share data across applications; there’s no common storage area that all Android packages can access. Android ships with a number of content providers for common data types (audio, video, images, personal Contact information, and so on) you can query these providers for the data they contain (although, for some, You must acquire the proper permission to read the data).

If you want to make your own data public, you have two options:

  1. You can create your own content provider.

  2. You can add the data to an existing provider — if there’s one that controls the same type of data and you have permission to write to it.

Steps to interact with existing Content Providers

  • Get a ContentResolver by calling getContentResolver() from within the implementation of an Activity or other application component.

java
@Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ownersearch);

            Cursor cursor = getContentResolver().query(County.CONTENT_URI,
                   new String[] { County.COUNTY_NAME }, null, null, null);


      }
  • You can then use the ContentResolver’s methods to interact with whatever content Providers you’re interested in.

  • The system instantiates all ContentProvider objects; you never need to do it on your own.

  • In fact, you never deal directly with ContentProvider objects at all. Typically, there’s just a single instance of each type of ContentProvider.

URI

Each content provider exposes a public URI (wrapped as an Uri object) that uniquely identifies its data set. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one.

All URIs for providers begin with the string "content://".

Tip
The content: scheme identifies the data as being controlled by a content provider.

If you’re defining a content provider, it’s a good idea to also define a constant for its URI, to simplify client code and make future updates cleaner. Android defines CONTENT_URI constants for all the providers that come with the platform.

Content URI Summary

summary
 content://com.myorg.trvajjala.countyinfoprovider/state/40
 <1>             <2>                              <3>   (4)

Each part in the Content Uri Explained below

1. Standard prefixes indicating that the data is controlled by a content provider. It’s never modified.

2. The authority part of the URI; it identifies the content provider. For third-party applications, this should be a fully-qualified class name (reduced to lowercase) to ensure uniqueness. The authority is declared in the <provider> element authorities attribute.

provider.xml
<provider android:authorities="com.adaequare.trvajjala.countyinfoprovider"
android:name=".CountyInfoProvider"></provider>

3. The path that the content provider uses to determine what kind of data is being requested. This can be zero or more segments long. If the content provider exposes only one type of data (only trains, for example), it can be absent. If the provider exposes several types, including subtypes, it can be several segments long — for example, "land/bus", "land/train", "sea/ship", and "sea/submarine

4. The ID of the specific record being requested, if any. This is the _ID value of the requested record. If the request is not limited to a single record, this segment and the trailing slash are omitted:

content://com.myorg.trvajjala.countyinfoprovider/state

Some of the default content providers by android

  • ContactsContract.CommonDataKinds.Phone.CONTENT_URI

(content://com.android.contacts/data/phones)

  • The Uri of the browser Bookmarks content provider is defined in

Browser.BOOKMARKS_URI (content://browser/bookmarks)

  • MediaStore.Video.Media.EXTERNAL_CONTENT_URI

(content://media/external/video/media) and so on.

Note
in order to work with these content providers you need to add appropriate permission in the android manifest file.
permission.xml
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

To read device contacts.

2 comments:

  1. Thanks for taking time to share this post. It is really useful. Continue sharing more like this.
    Regards,
    Android Training in Bangalore

    ReplyDelete