Android Content Provider

Source: Internet
Author: User

Provider Data Type
Contentproviders can recognize multiple data types. User dictionaries only recognize text types, but other providers can recognize the following formats:

Integer

Long (long)

Floating Point

Long floating point (double)

The data type frequently used by another provider is "BLOB", which is like a 64 kB byte array. you can use the "get" method of the Cursor class to view available data types.

The Data Types of each column in the provider are listed in their files. each data type of the user dictionary Provider is in its contract class UserDictionary. the references for the Words (Contract classes are described in the ContractClasses section) are listed. you can also call Cursor. getType () to determine the data type.

The Provider also maintains MIME data type information for each contentURI. you can use MIME information to determine whether your application can process the information provided by the provider, or select a data type to be processed based on the MIME information. when you use a provider that contains complex data structures or files, you often need MIME information. for example, in the contact Provider table ContactsContract. data uses the MIME type to mark each row of the stored contact Data. to obtain the MIME type of a contentURI, call ContentResolver. getType ().

Thesection MIME type reference section describes the standard and custom MIME type syntax.

 

 

Three types of operation providers
In application development, there are three types of important operation providers:

Batchaccess: You can use the ContentProviderOperation class method to create a batch of operation calls, and then use ContentResolver. applyBatch () to apply them.

Asynchronousqueries: You need to execute the request in another thread. This method uses a CursorLoader object. The example in the Loaders guide demonstrates how to do this.

Dataaccess via intents: although you cannot directly send intent to a provider, you can send intent to the provider application, which is usually the best way to modify the data of provider.

You can use the intent batch operation and modification methods in the following sections.

 

 

Batch operation

Batch operations on a provider are useful when inserting a large number of rows. They can also be used to insert multiple rows into multiple tables in a method call, or is used to execute a series of operations as a transaction (an atomic operation ).

To operate a provider in "batch mode", you need to create an array of ContentProviderOperation objects and then use ContentResolver. applyBatch () sends it to a contentprovider. you should pass the contentprovider authority instead of the general contentURI to this method. authority allows each ContentProviderOperation object in the array to act on different tables. for ContentResolver. the call of applyBatch () will return an array storage result.

The description of the Contract class ContactsContract. RawContacts contains a code snippet to demonstrate batch insertion. The ContactManager application example contains a batch operation example in its ContactAdder. java source code file.

 

 

Use other applications to display data

If your application has operation permissions, you may still want to use an intent to display data in other applications. for example, the calendar application accepts an ACTION_VIEWintent and then displays a date or event. this allows you to display a date, but you do not need to create a page by yourself. for more information, see the CalendarProvider guide.

The target application you send intent to is not necessarily a provider-related application. for example, you can obtain a contact from the contact Provider, and then send an ACTION_VIEWintent containing the contact image to an image display application.

 

 

Operate data through intent
Intents can provide indirect contentprovider operations. therefore, your application can operate on data in providers that do not have permissions. this can be achieved by obtaining a returned intent from an authorized application or activating an authorized application.

 

 

Use temporary permissions

Even if you do not have the appropriate permissions, you can still operate the data in contentprovider, you only need to send an intent to an application with the permission and then obtain a returned intent containing the "URI" permission. the permissions of a specific URI will continue until the activity that gets them ends. applications with permissions grant temporary permissions by setting a flag in the intent to be returned:

Read Permission: FLAG_GRANT_READ_URI_PERMISSION

Write Permission: FLAG_GRANT_WRITE_URI_PERMISSION

Note: These flag do not grant read and write permissions to the provider corresponding to the authority contained in contentURI. The operation is only for the URI itself.

A provider defines the URI permissions of contentURI in its manifest, and uses the android: grantUriPermission attribute of the <provider> element and the <grant-uri-permission> attribute of the <provider> sub-element. the URI permission mechanism is described in the "URI permission" section in the Securityand Permissions guide.

For example, you can obtain a contact data from ContactsProvider, even if you do not have the READ_CONTACTS permission. you may want to do this in an application that sends an e-card to someone on their birthday. your preferred choice is to allow users to control only the contacts used by your applications, rather than directly asking READ_CONTACTS permissions to operate on all contacts and all their information. to do this, you need to perform the following process:

Your application sends an intent that contains the actionACTION_PICK and "contacts" MIME-type CONTENT_ITEM_TYPE. Use startActivityForResult ().

Because the intent matches leleapp's "selection" activity, the activity will be placed at the front end.

In selectionactivity, select a contact to update. in this case, selectionactivity calls setResult (resultcode, intent) to create an intent and return it to your application. intent contains the contentURI of the selected contact and the "extras" flag FLAG_GRANT_READ_URI_PERMISSION. this flag grants your application URI permission to read data from contacts specified by the URI. this selectionactivity then calls finish () to control the application returned to you.

Your activity is returned to the foreground, and the system calls the onActivityResult () method of your activity. This method receives the intent created and returned by selectionactivity in Peopleapp.

Using the contentURI in the returned intent, you can read contactdata from ContactsProvider, even if you do not have the permanent Provider read permission in your manifest. then you can get the contact's birthday information and his email address and then send him an e-card.

 

 

Use other applications

A simple way to enable a user to modify data that your application does not have permissions is to activate an application with permissions and then let the user operate on it.

For example, the calendar application receives an ACTION_INSERTintent, enabling you to activate the insert interface of the calendar application. you can transmit "extra" data in this intent and use it to preset the interface. because the returned event has a complex syntax, the best way to insert an event to the Calendar Provider is to use ACTION_INSERT to activate the Calendar application and then let the user Insert the event on it.


 

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.