android content providers pdf by apk855

VIEWS: 39 PAGES: 9

More Info
									        Content Providers



CONTENT PROVIDERS-1   Copyright © 2012,apkdownloads.in, All rights reserved
    • The above data storage mechanisms. i.e. using
      SharedPreferences, File Systems, SD card storage
      or using SQLite Database, are used to store the data
      and the data is available only to the package that is
      created.
    • Android supports sharing data around multiple
      packages by using the content providers.
    • Android supports some built-in content providers
      and also supports the user to create userdefined
      content providers.
    • Think content provider as a data store.
    • It is not important how it stores the data rather it is
      important to know how the data is shared between
      the packages.
CONTENT PROVIDERS-2                   Copyright © 2012,apkdownloads.in, All rights reserved
    • Content providers behave very much like databases.- you
      can query it, edit its content, as well as add or delete its
      content.
    • The content provider can use different ways of storing
      data.
    • The data can be stored in a database or in a file system or
      in a network.
    • Android ships with a number of content providers for
      common data types.
            audio
            video
            images
            personal contact information
             and so on…..
    • To query a content provider, you provide a query string in
      the form of a URI

CONTENT PROVIDERS-3                         Copyright © 2012,apkdownloads.in, All rights reserved
          <standard_prefix>://<authority>/<data_path>/<id>
     URI part                Description
     <standard_prefix>       This is the standard prefix for content providers is
                             always content://
     <authority>             Specifies the name of the content providers.
                             contacts: for built-in Contacts for Content Providers .
                             For Third party content providers it would be
                             qualified names such as: com.wrox.provider
     <datapath>              Specifies the kind of the data requested.
                             For Example: if you getting all the contacts from the
                             Contacts content provider, then the data path would
                             be people, and the URI looks like:
                             content://contacts/people.
     Id                      Specifies the specific record requested. For example,
                             if you are looking for contact
                             number 2 in the Contacts content provider, the URI
                             would look like this:
                             content://contacts/people/2.

CONTENT PROVIDERS-4                                  Copyright © 2012,apkdownloads.in, All rights reserved
                               Example Query Strings

    Query String                        Description

    content://media/internal/images     Returns a list of all the internal images on
                                        the device

    content://media/external/images     Returns a list of all the images stored on
                                        the external
                                        storage (e.g., SD card) on the device


    content://call_log/calls            Returns a list of all calls registered in the
                                        Call Log

    content://browser/bookmarks         Returns a list of bookmarks stored in the
                                        browser



CONTENT PROVIDERS-5                             Copyright © 2012,apkdownloads.in, All rights reserved
          Example program for retrieving the contacts
                information from your mobile
    •   Format the main.xml file.
         <?xml version=”1.0” encoding=”utf-8”?>
         <LinearLayout
         xmlns:android=”http://schemas.android.com/apk/res/android”
         android:orientation=”vertical” android:layout_width=”fill_parent”
         android:layout_height=”fill_parent” >
         <ListView
         android:id=”@+id/android:list” android:layout_width=”fill_parent”
         android:layout_height=”wrap_content” android:layout_weight=”1”
         android:stackFromBottom=”false” android:transcriptMode=”normal”
         />
         <TextView
         android:id=”@+id/contactName” android:textStyle=”bold”
         android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
         />
         <TextView
         android:id=”@+id/contactID” android:layout_width=”fill_parent”
         android:layout_height=”wrap_content” />
         </LinearLayout>
CONTENT PROVIDERS-6                           Copyright © 2012,apkdownloads.in, All rights reserved
                            Activity java file
    Uri allContacts = Uri.parse(“content://contacts/people”);
    Cursor c = managedQuery(allContacts, null, null, null, null);
    String[] columns = new String[] {
       ContactsContract.Contacts.DISPLAY_NAME,
       ContactsContract.Contacts._ID};
       int[ ] views = new int[] {R.id.contactName,
                     R.id.contactID};
    SimpleCursorAdapter adapter =
       new SimpleCursorAdapter(this, R.layout.main, c, columns,
       views);
    this.setListAdapter(adapter);
    • We have to take the permission for reading the contacts.
    • Permission can be written in AndroidManifest.xml file.
    <uses-permission
       android:name=”android.permission.READ_CONTACTS”>
    </uses-permission>

CONTENT PROVIDERS-7                        Copyright © 2012,apkdownloads.in, All rights reserved
                      User Defined Content Providers
    • User defined Content providers must extends the
      class i.e. ContentProvider.
    • The various methods to override in this class are as
      follows:
        Method                     Description
        getType()                  Returns the MIME type of the data at
                                   the given URI
        onCreate()                 Called when the provider is started
        query()                    Receives a request from a client. The
                                   result is returned as a Cursor object
        insert()                   Inserts a new record into the content
                                   provider
        delete()                   Deletes an existing record from the
                                   content provider
        update()                   Updates an existing record from the
                                   content provider
CONTENT PROVIDERS-8                        Copyright © 2012,apkdownloads.in, All rights reserved
    • Configure   the    Content      provider     in                              the
      AndroidManifest.xml file with the following tag
          <provider>
    • Example:
          <provider android:name=”BooksProvider”
           android:authorities=”net.learn2develop.provider.Books” />




CONTENT PROVIDERS-9                        Copyright © 2012,apkdownloads.in, All rights reserved

								
To top