Android local content provider

Source: Internet
Author: User

Android exposes many content providers to access local databases.

 

You can use the techniques described earlier to use the local content provider. The android. provider class contains some convenient classes to simplify these useful providers, including:

 

Browser

Browser content provider can be used to read or modify tags, view history, or search by network.

 

❑ Calllog

View or update the phone history, including incoming and outgoing calls, missed calls, and phone details, such as contacts and call times.

 

❑ Contacts

The contacts provider can be used to read, modify, or maintain contact information.

 

❑ Mediastore

Mediastore provides centralized control over multimedia files on devices, including audio, video, and images. You can save your multimedia in mediastore for global access.

 

Administrative settings

You can use settings provider to access the preference of a device. You can use it to view and modify Bluetooth settings, ringtones, and other device settings.

 

 

You should try to use these local content providers to ensure that your programs are integrated with local or third-party programs without flaws.

 

This section describes how to use these helper classes outside the scope of this Chapter. The following section describes how to use some of the useful and powerful local content providers.

 

Use mediastore provider

 

Android mediastore provides a management repository for audio, video, and image files. When you add a new multimedia file to the android file system, you should also add it to mediastore to ensure that it can be visible to other programs.

 

The mediastore class contains some convenient methods to simplify file insertion. For example. The following code snippet shows how to directly insert a file to mediastore:

 

Android. provider. mediastore. Images. Media. insertimage (getcontentresolver (),

Sourcebitmap, "my_cat_pic", "photo of my cat !");

 

Use contacts provider

 

Access to the Contact Manager is particularly important on communication devices. Android has done the right thing at this point: any application that grants read_contacts permission can access all information in the contact database.

 

In the following example, activity obtains the cursor of each person in the contact database and creates a string array to save the name and phone number of each contact.

 

To simplify data extraction from cursor, Android provides static attributes of the People class to represent column names.

 

// Get a cursor over every contact.

Cursor cursor = getcontentresolver (). Query (people. content_uri, null );

 

// Let the activity manage the cursor lifecycle.

Startmanagingcursor (cursor );

 

// Use the convenience properties to get the index of the columns

Int nameidx = cursor. getcolumnindexorthrow (people. Name );

Int phoneidx = cursor. getcolumnindexorthrow (people. number );

String [] result = new string [cursor. getcount ()];

If (cursor. movetofirst ())

Do {

// Extract the name.

String name = cursor. getstring (nameidx );

 

// Extract the phone number.

String phone = cursor. getstring (phoneidx );

 

Result [cursor. getposition ()] = Name + "(" + phone + ")";

} While (cursor. movetonext ());

 

To run this code snippet, you need to add the read_contacts permission to the program.

 

Like querying the contact database, you can also use this content provider to modify, delete, or insert contact records.

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.