Android Contentresolve Use

Source: Internet
Author: User

Use Contentresolve to access data from other programs in Android:

Http://developer.android.com/reference/android/content/ContentProvider.html

Use Contentresolve to access your phone's contacts

Mainactivity.java: Note the use of the Getcontentresolver.query method is actually parsing a URI

 Packageorg.elvalad.contactstest;Importandroid.app.Activity;ImportAndroid.database.Cursor;ImportAndroid.os.Bundle;Importandroid.provider.ContactsContract;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsActivity {ListView contactsview; Arrayadapter<String>adapter; List<String> contactslist =NewArraylist<string>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Contactsview=(ListView) Findviewbyid (R.id.contacts_view); Adapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, contactslist);        Contactsview.setadapter (adapter);    Readcontacts (); }    Private voidreadcontacts () {cursor cursor=NULL; Try{cursor= Getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);  while(Cursor.movetonext ()) {//get name of contact personString DisplayName =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); //Get contact phone numberString number =cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Contactslist.add (DisplayName+ "\ n" +Number ); }        } Catch(Exception e) {e.printstacktrace (); } finally {            if(Cursor! =NULL) {cursor.close (); } }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity ">    <ListViewAndroid:id= "@+id/contacts_view"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    </ListView></LinearLayout>

Androidmanifest.xml: Increase Read Contact permissions

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Org.elvalad.contactstest" >    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application>        <uses-permissionAndroid:name= "Android.permission.READ_CONTACTS" /></Manifest>

Android Contentresolve Use

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.