📜 ⬆️ ⬇️

Content providers. Interaction with contacts

image
Good day, dear Habrovchane!

As you all know - Android is a potentially developing and competitive operating system. Developers under this system every day becomes more and more. Even though I’m a novice Android developer, I’m not going to tell you how to install Eclipse and ADT under it.

In this topic, I would like to talk about content providers in the Android operating system and give a small example of working with one of them.
')


What are content providers?

Content providers (content providers) - entities designed to summarize data in services. Through the use of content providers, data sources become similar to view-state-equipped view state transfer (REST) ​​data providers
image



What is REST

REST (Representational state transfer) is a software architecture style for distributed systems, such as the World Wide Web, which is typically used to build web services. The term REST was introduced in 2000 by Roy Fielding, one of the authors of the HTTP protocol. Systems that support REST are called RESTful systems.

In general, REST is a very simple information management interface without the use of any additional internal layers. Each piece of information is uniquely identified by a global identifier, such as a URL. Each URL in turn has a strictly specified format.

And now the same thing is more obvious:

The absence of additional internal layers means the transfer of data in the same form as the data itself. Those. we do not wrap the data in XML, as SOAP and XML-RPC do, do not use AMF, as Flash does, etc. Just give yourself the data.

Each unit of information is uniquely identified by a URL - this means that the URL is essentially the primary key for the data unit. Those. for example, the third book from the bookshelf will look like / book / 3, and 35 the page in this book will be / book / 3 / page / 35. From here it turns out a strictly specified format. And it doesn't matter at all what format the data is located at / book / 3 / page / 35 - it can be HTML, a scanned copy in the form of a jpeg-file, and a Microsoft Word document.

How service information is managed is entirely based on the data transfer protocol. The most common protocol is of course HTTP. So, for HTTP, the action on data is set using the methods: GET (get), PUT (add, replace), POST (add, change, delete), DELETE (delete). Thus, CRUD (Create-Read-Update-Delete) actions can be performed with all 4 methods, or only with GET and POST.

Here is how it will look like by example:

GET / book / - get a list of all books
GET / book / 3 / - get the book number 3
PUT / book / - add a book (data in the request body)
POST / book / 3 - change the book (data in the request body)
DELETE / book / 3 - delete a book


Types of standard content providers in Android

   - Browser
   - CallLog
   - Contacts
     - People
     - Phones
     - Photos
     - Groups
   - MediaStore
     - Audio
         - Albums
         - Artists
         - Genres
         - Playlists
     - Images
         - Thumbnails
     - Video
   - Settings

SQLite databases are located at the top of the hierarchy, and tables at the bottom. Therefore, Browser, CallLog, Contacts, MediaStore, Settings are separate SQLite databases encapsulated in the form of providers.


Content Providers Architecture

Each content provider is registered in the device as a website, using a special string (it looks like a domain name and is called authority). This unique sequence of character set in a device is the basis for a set of URIs.

The registration of the source is carried out in the file AndroidManifest.xml. Below are two examples of how content providers can be registered with Android.

<provider android:name = "SomeProvider" android:authorities = "com.YourApplication.SomeProvider" />
<provider android:name = "NotePadProvider" android:authorities = "com.google.provider.NotePad" />

Content providers will also encounter similar REST links for retrieving content:

content: //com.google.provider.NotePad/notes/ - getting all records
content: //com.google.provider.NotePad/notes/# - getting one record by ID

Below are some more URIs for content providers.

content: // media / internal / images /
content: // media / external / images /
content: // contacts / people /
content: // contacts / people / 23

Here, the providers (content: // media /) and (content: // contacts) are not fully structured, which means that these content providers are not third-party and are controlled by the Android system.

Consider a specific URI in more detail:

content: //com.google.provider.NotePad/notes/23

After content: the URI contains the uniform source identifier, which is used to locate the content provider in the corresponding registry.

In this URI, the com.google.provider.NotePad part is the source.
/ notes / 23 is a section of the path section specific to each individual content provider.


Read contact data using a URI

It is important to remember about the security system in Android, so add to the AndroidManifest.xml file:

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

More details about access rights can be read here.

Consider the following URIs

People.CONTENT_URI

The equivalent text URI string will be:

content: // contacts / people /

When using such URIs, the code to get the strings will look like this:

 /*     */ Cursor managedCursor = managedQuery(People.CONTENT_URI, new String[] { People._ID, People.NAME, People.NUMBER }, //    null, //   () null, //   People.NAME + " ASC"); //   //   : startManagingCursor(managedCursor); // ,      if (managedCursor.getCount() > 0) { while (managedCursor.moveToNext()) { //     Log.i("DATA", managedCursor.getString(0) + " :: " //   _ID + managedCursor.getString(1) + " " //   NAME + managedCursor.getString(2)); //   NUMBER } } 


More about cursors here .

Source: https://habr.com/ru/post/132720/


All Articles