views:

335

answers:

2

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?

+1  A: 

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.

Roman Nurik
I went over the developer guide, but was anware of the Note Pad samples, thanks.
tipu
Glad to help! We're working on surfacing and raising awareness of these types of resources better.
Roman Nurik
Regarding the second part of the question... how do other applications use or import the ContentProvier? The NotePad example does not cover this point. Thanks.
Guido
A: 

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"

TwistedUmbrella