Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Content Provider in Android
Content Provider in Android
Content Provider in Android


by Rohit Kesharwani on 10/15/2011 3:02:00 PM

Views: 2099       Comments: 0

Content Provider in Android

ContentProvider are used to provide data from an application to another. ContentProvider do not store the data but provide the interface for other applications to access the data.

The following example will use an existing context provider from "Contacts".

Create contacts on your emulator

For this example we need a few maintained contacts. Select the home menu and then the menu entry "Contacts" to create contacts.

Content Provider in Android

Press Menu and select "New Contact".

Content Provider in Android

As a result you should have a few new contacts.

Content Provider in Android

Using the Contact Content Provider

Create a new Android project "com.android.contentprovider" with the activity "ContactsView".

Rename the id of the existing TextView from the example wizard to "contactview". Delete the default text. Also change the layout_height to "fill_parent".

The resulting main.xml should look like the following.

<?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">

       <TextView android:layout_width="fill_parent"

              android:layout_height="fill_parent" android:id="@+id/contactview" />
</
LinearLayout>

Access to the contact content provider requires a certain permission as not all applications should have access to the contact information. Open the AndroidManifest.xml, and select the Permissions tab. On that tab click the "Add" button, and select "Uses Permission". From the drop-down list select the entry "android.permission.READ_CONTACTS".

Content Provider in Android

Change the coding of the activity.

package com.android.contentprovider;

 

import android.app.Activity;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.widget.TextView;

 

public class ContactsView extends Activity {

    /** Called when the activity is first created. */

       @Override

       public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.main);

          TextView contactView = (TextView) findViewById(R.id.contactview);

          Cursor cursor = getContacts();

          while (cursor.moveToNext())

   {

              String displayName = cursor.getString(cursor     
                                .getColumnIndex(ContactsContract.Data.
DISPLAY_NAME));

              contactView.append("Name: ");

              contactView.append(displayName);

              contactView.append("\n");

          }

       }

 

       private Cursor getContacts() {

              // Run query

              Uri uri = ContactsContract.Contacts.CONTENT_URI;

              String[] projection = new String[] { ContactsContract.Contacts._ID,

                           ContactsContract.Contacts.DISPLAY_NAME };

              String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"

                           + ("1") + "'";

              String[] selectionArgs = null;

              String sortOrder = ContactsContract.Contacts.DISPLAY_NAME

                                 + " COLLATE LOCALIZED ASC";

 

              return managedQuery(uri, projection, selection, selectionArgs,

                                  sortOrder);

       }

}

Right click on the project à Select Run As à Android Application.

Content Provider in Android

When you run your application it will show you all the names of the available contacts.

Thanks.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 7103
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.