Android Content provider detailed and sample code _android

Source: Internet
Author: User

The use of Android:content provider.

1. Content Provider Introduction

2, the use of ready-made content Provider

3. Define your own content Provider

First, Content Provider introduction

We say the four core components of the Android app are activity, Service, broadcast Receiver, and Content Provider. In Android, applications are independent of each other, and they all run in their own stand-alone virtual machines. Content Provider provides a way to share data between programs, a program can use the content Provider to define a URI, provide a unified operation interface, other programs can access the specified data through this URI, data increase, delete, change, check.

Second, the use of ready-made content Provider

Let's take a sample of the content Provider from the Android address book to illustrate how to use ready-made content Provider.

1, a new project Lesson20_contentprovider project.

2, Res/layout/main.xml content omitted, is to make a query button.

3, the contents of the Maincontentprovider.java are as follows:

Package android.basic.lesson20;
Import android.app.Activity;
Import Android.content.ContentResolver;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import Android.widget.Toast;
    The public class Maincontentprovider extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override
        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.main);

        Button B1 = (button) Findviewbyid (R.ID.BUTTON01);
                Onclicklistener OCL = new Onclicklistener () {@Override public void OnClick (View v) {
                Contentresolver contentresolver = Getcontentresolver (); Get all the contacts Cursor Cursor = Contentresolver.query (contactscontract.
                Contacts.content_uri, NULL, NULL, NULL, NULL); Loop traversal if (Cursor.movetofirst ()) {int idcolumn = Cursor.getcolumnindex (contactscont Ract.

                    CONTACTS._ID);

                    int displaynamecolumn = Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);

                        Do {//Get Contact ID number String ContactID = cursor.getstring (Idcolumn);

                        Get Contact name String DisPlayName = cursor.getstring (Displaynamecolumn); Toast.maketext (maincontentprovider.this, "Contact person Name:" +displayname, Toast.length_

                        LONG). Show (); View how many phone numbers the contact has. If not, the return value is 0 int phonecount = cursor.getint (Cursor.getcolumnindex ContactsContract.Contacts.HAS_PHON

                        E_number));
               if (Phonecount > 0) {//Get contact phone number list             Cursor phonescursor = Getcontentresolver (). Query (Contactscontract.commondatakinds.phone.content_uri,null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID

                            + "=" + ContactID, NULL, NULL);
                                    if (Phonescursor.movetofirst ()) {do {//traverse all phone numbers String PhoneNumber = phonescursor. getString (Phonescursor. Getcolumnindex (CONTACTSCONTRACT.COMMONDATAKINDS.PHONE.N
                                    umber)); Toast.maketext (Maincontentprovider.this, "Contact Tel:" +phonenumber, Toast.length_lo
                                NG). Show ();
                            while (Phonescursor.movetonext ());

                 }
                        }   while (Cursor.movetonext ());

        }
            }
        };
    B1.setonclicklistener (OCL); }

}

Add in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.GET_ACCOUNTS"/>
     <uses-permission android:name= " Android.permission.READ_CONTACTS "/>

4, realize the effect chart:

The above is a simple example of Android Content provider, follow-up continue to supplement the relevant knowledge, thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.