Custom content providers in android

I am trying to create my own ContentProvider so that more than one application (activity?) Can access it. I have a few questions on how to do this,

How do I declare in code that it is a ContentProvider? How do other apps (actions?) Use or import the ContentProvider?

+2


a source to share


2 answers


In addition to the extensive developer guide section on content providers , you can check out the Notepad Tutorial and Note Pad Sample Code for information on creating your own content providers.



+4


a source


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 receiving the 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 will return the item specified by item_socks, but this is just a general example of having only one item specified by that name. You will be querying a database table named database_items that looks something like this:

id | name | text

1 | item_socks | brown couple red stripe

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

0


a source







All Articles