tipu
tipu

Reputation: 9604

custom content providers in android

I'm attempting to make a custom ContentProvider so that more than one application (activity?) has access to it. I have several questions about how to do this,

How do I declare in the code that it is a ContentProvider? How do other applications (activities?) use or import the ContentProvider?

Upvotes: 2

Views: 6400

Answers (2)

Abandoned Cart
Abandoned Cart

Reputation: 4676

In the manifest of the application providing the content you would have:

<provider android:name="Inventory" android:authorities="com.package.name.Inventory" />

And in the application obtaining content

    String inventoryItem = "dunno";
       try {
          Uri getItem = Uri.parse(
            "content://com.package.name.Inventory/database_items");
            String[] projection = new String[] { "text" };
            Cursor cursor = managedQuery(getItem, projection, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
              int index = cursor.getColumnIndex("text");
              inventoryItem = cursor.getString(index));     
            }
            cursor.close();
       } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }

In an external program, this would return the item listed under item_socks, but this is just a general example of having only one item listed with that name. You would be querying a database table named database_items that looks something like:

id | title | text

1 | item_socks | brown pair red stripe

inventoryItem would then be equal to "brown pair red stripe"

Upvotes: 0

Roman Nurik
Roman Nurik

Reputation: 29745

Aside from the extensive developer guide topic on content providers, you can look at the Notepad Tutorial and Note Pad sample code for information on creating your own content providers.

Upvotes: 4

Related Questions