Use of content providers in Android

Source: Internet
Author: User

Content providers in Android are primarily used for data sharing between different programs. There are two general types of content providers, one is to use an existing content provider to read and manipulate data for the corresponding program, and to create your own content provider for other programs to access.

Use an existing content provider to read and manipulate data for the corresponding program

Contentresolve provides a series of methods for adding and removing data, where insert () is used to add data, update () is used for data updates, and delete () methods are used to delete data and query () queries for data.

In Contentresolve, the content URI is used to determine the resource, and the URI consists of two parts, the permissions and the path. Permissions are generally composed of application +provider, and the path is used to distinguish between different tables under the same program, usually appended to the permissions. The full content URI is also added to the Protocol declaration, such as "Content://com.example.app.provider/table1". When we get the URI string, we want to parse it into a URI object using the Uri.parse () method.

The query () method requires the passing of five parameters. The first URI specifies that a table under one application is queried, the second parameter specifies the column name of the query, and third. The four parameters specify the query criteria, and the fifth parameter specifies the order of the permutations. The method returns a cursor object that iterates through the cursor object and extracts the corresponding data.

The Insert () method requires the passing of two parameters. The first is the URI, and the second is the Contentvalue object, which specifies the data to be inserted

The update () method needs to pass in four parameters, the first is the URI, the second is the Contentvalue object, and the third to fourth parameter specifies the modified data.

The delete () method requires three parameters. The first is a URI, and the second to third is the constraint that specifies the object to delete.

Here is a look at the specific code of the contact person:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"4 Android:paddingright= "@dimen/activity_horizontal_margin"5 Android:paddingtop= "@dimen/activity_vertical_margin"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Tools:context=". Mainactivity "8 android:orientation= "vertical"9     >Ten  One     <ListView A         Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" - Android:id= "@+id/contacts_view" the         ></ListView> -  - </LinearLayout>
Activity_main.xml

Java code

1  Public classMainactivityextendsappcompatactivity {2 3 4 ListView Contactsview;5Arrayadapter<string>adapter;6 7List<string> contactslist =NewArraylist<string>();8 9 Ten @Override One     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Contactsview =(ListView) Findviewbyid (R.id.contacts_view); theadapter =NewArrayadapter<string> ( This, Android. r.layout.simple_list_item_1,contactslist); - Contactsview.setadapter (adapter); -  - readcontacts (); +  -     } +  A     Private voidreadcontacts () { atcursor cursor =NULL; -         Try { -cursor =getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, -                     NULL,NULL,NULL,NULL); -              while(Cursor.movetonext ()) { -String name =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); inString number =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); -Contactslist.add (name+ "\ n" +Number ); to             } +  -}Catch(Exception e) { the e.printstacktrace (); *         } $     }Panax Notoginseng  -  the}
Mainactivity.java

Because you want to access contacts, you must add permissions.

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

Use of content providers in Android

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.