ContactsContract如何枚举所有可用字段?

时间:2022-09-09 23:21:20

In my application I need to give user opportinity to see/edit all available fields of ContactsContract.

在我的应用程序中,我需要为用户提供查看/编辑ContactsContract的所有可用字段的机会。

How should I get/see all available fields - including any custom ones?

我应该如何获取/查看所有可用字段 - 包括任何自定义字段?

2 个解决方案

#1


10  

First get all of the names of people that exist in your phone:

首先获取手机中存在的所有人的姓名:

Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    null,
                    null,
                    ContactsContract.Contacts.DISPLAY_NAME
                            + " COLLATE LOCALIZED ASC");
            while (phones.moveToNext()) {

                name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                namelist.add(name);
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 number_list.add(phoneNumber);
            detaillist.put(name, phoneNumber);
            }
            phones.close();
}

Now get contact id and get other information like phone number, email id, address, organization by using contact id:

现在获取联系人ID并使用联系人ID获取电话号码,电子邮件ID,地址,组织等其他信息:

protected void get_UserContactId(String item_clicked) 
        {
            System.out.println("i m in fincution contact id");
            ContentResolver localContentResolver = this.getContentResolver();
            Cursor contactLookupCursor =  
               localContentResolver.query(
                        Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                        Uri.encode(item_clicked)), 
                        new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                        null, 
                        null, 
                        null);
            try {
            while(contactLookupCursor.moveToNext()){
              contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                user_contact_id = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));

                 System.out.println("contatc id id"+user_contact_id);

                }

            }
        finally {
            contactLookupCursor.close();
            } 

        }


protected void get_UserEmail(String item_clicked) 
        {
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user_contact_id, null, null); 
            while (emails.moveToNext()) { 
               // This would allow you get several email addresses 
               user_email_id = emails.getString( 
               emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
               String user_email_type=emails.getString( 
                       emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
               System.out.println("the email type"+user_email_type);
               System.out.println("email address might be"+emailAddress);
            contact_attribute_type.add(EMAIL_TYPE[(Integer.parseInt(user_email_type))-1]);
            contact_attribute_value.add(user_email_id);
            } 
            emails.close(); 

        }

        protected void get_UserNumber(String item_clicked) {
            // TODO Auto-generated method stub
            final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.STARRED,
                ContactsContract.Contacts.TIMES_CONTACTED,
                ContactsContract.Contacts.CONTACT_PRESENCE,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.Contacts.LOOKUP_KEY,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
            };



            String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +item_clicked+ "\" )";
            Cursor c = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
            this.startManagingCursor(c);

            if (c.moveToNext())
            {
                String id = c.getString(0);
                ArrayList<String> phones = new ArrayList<String>();

                Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while (pCur.moveToNext())
                {
                    phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    user_number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   String number_type=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                   System.out.println("the number type---"+number_type); 
                   System.out.println("user no. in share is"+user_number);
                   contact_attribute_type.add(PHONE_TYPE[(Integer.parseInt(number_type))-1]);
                   contact_attribute_value.add(user_number);
                 //   Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

                } 
                pCur.close();   
            }

            }


        protected String get_UserAddress() {
            // TODO Auto-generated method stub
            try { 

                System.out.println(" i m in user address");
            Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + client_contact_id, null, null); 
                while (address.moveToNext()) { 
                   // This would allow you get several email addresses 
                  user_home_address = address.getString( 
                   address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA)); 
                  String user_address_type=address.getString( 
                           address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                  System.out.println("the user address type"+user_address_type);
                  System.out.println(" address might be"+user_home_address);
                contact_attribute_type.add(ADDRESS_TYPE[(Integer.parseInt(user_address_type))-1]);
                contact_attribute_value.add(user_home_address);

                } 
                address.close(); 
            }
            catch(Exception e)
            {
                System.out.println("this exception due to website"+e);
            }

        return(user_home_address);  
        }   


          protected void get_UserWebsite() {
                // TODO Auto-generated method stub
                        try{
                            Cursor websiteNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Website.URL}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                    + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
                                    + "'",null,null);

                            websiteNameCursor.moveToNext();
                            user_website=(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL)));

                            System.out.println("this is my website"+(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL))));
                        contact_attribute_type.add("Website");
                        contact_attribute_value.add(user_website);

                        }
                            catch(Exception e)
                            {
                            user_website=null;
                            System.out.println("this website is"+user_website); 
                            System.out.println("this exception in website"+e);
                            }



            }

                    protected void get_UserOrganization() {
                // TODO Auto-generated method stub
                        try{
                            Cursor organizationNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Organization.COMPANY}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                    + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
                                    + "'",null,null);

                            organizationNameCursor.moveToNext();
                            user_company=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.COMPANY));
                        //    String user_company_type=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TYPE));
                       //   System.out.println("the company type "+user_company_type);
                            System.out.println("this is my organization"+user_company);

                            contact_attribute_type.add("Company");
                        contact_attribute_value.add(user_company);
                        }
                            catch(Exception e)
                            {
                            user_company=null;
                            System.out.println("user company name is"+user_company);
                                System.out.println("this exception in org"+e);
                            }
            }   




    }

By doing this you can get all the fields of contacts-contract.

通过这样做,您可以获得联系人合同的所有字段。

#2


1  

This logs the name and value of all columns in all contact rows. Tested on Galaxy S6 with native contacts app. I needed it to find the column name that is used for custom SMS notification sound (sec_custom_alert).

这会记录所有联系人行中所有列的名称和值。使用原生联系人应用程序在Galaxy S6上测试。我需要它来查找用于自定义SMS通知声音的列名称(sec_custom_alert)。

private void enumerateColumns() {
    Cursor cursor = getApplicationContext().getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            for(int i=0; i<cursor.getColumnCount();i++)
            {
                Log.d("myActivity", cursor.getColumnName(i) + " : " + cursor.getString(i));
            }
        } while (cursor.moveToNext());

        cursor.close();
    }
}

#1


10  

First get all of the names of people that exist in your phone:

首先获取手机中存在的所有人的姓名:

Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    null,
                    null,
                    ContactsContract.Contacts.DISPLAY_NAME
                            + " COLLATE LOCALIZED ASC");
            while (phones.moveToNext()) {

                name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                namelist.add(name);
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 number_list.add(phoneNumber);
            detaillist.put(name, phoneNumber);
            }
            phones.close();
}

Now get contact id and get other information like phone number, email id, address, organization by using contact id:

现在获取联系人ID并使用联系人ID获取电话号码,电子邮件ID,地址,组织等其他信息:

protected void get_UserContactId(String item_clicked) 
        {
            System.out.println("i m in fincution contact id");
            ContentResolver localContentResolver = this.getContentResolver();
            Cursor contactLookupCursor =  
               localContentResolver.query(
                        Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                        Uri.encode(item_clicked)), 
                        new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                        null, 
                        null, 
                        null);
            try {
            while(contactLookupCursor.moveToNext()){
              contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                user_contact_id = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));

                 System.out.println("contatc id id"+user_contact_id);

                }

            }
        finally {
            contactLookupCursor.close();
            } 

        }


protected void get_UserEmail(String item_clicked) 
        {
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user_contact_id, null, null); 
            while (emails.moveToNext()) { 
               // This would allow you get several email addresses 
               user_email_id = emails.getString( 
               emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
               String user_email_type=emails.getString( 
                       emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
               System.out.println("the email type"+user_email_type);
               System.out.println("email address might be"+emailAddress);
            contact_attribute_type.add(EMAIL_TYPE[(Integer.parseInt(user_email_type))-1]);
            contact_attribute_value.add(user_email_id);
            } 
            emails.close(); 

        }

        protected void get_UserNumber(String item_clicked) {
            // TODO Auto-generated method stub
            final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.STARRED,
                ContactsContract.Contacts.TIMES_CONTACTED,
                ContactsContract.Contacts.CONTACT_PRESENCE,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.Contacts.LOOKUP_KEY,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
            };



            String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +item_clicked+ "\" )";
            Cursor c = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
            this.startManagingCursor(c);

            if (c.moveToNext())
            {
                String id = c.getString(0);
                ArrayList<String> phones = new ArrayList<String>();

                Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while (pCur.moveToNext())
                {
                    phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    user_number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   String number_type=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                   System.out.println("the number type---"+number_type); 
                   System.out.println("user no. in share is"+user_number);
                   contact_attribute_type.add(PHONE_TYPE[(Integer.parseInt(number_type))-1]);
                   contact_attribute_value.add(user_number);
                 //   Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

                } 
                pCur.close();   
            }

            }


        protected String get_UserAddress() {
            // TODO Auto-generated method stub
            try { 

                System.out.println(" i m in user address");
            Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + client_contact_id, null, null); 
                while (address.moveToNext()) { 
                   // This would allow you get several email addresses 
                  user_home_address = address.getString( 
                   address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA)); 
                  String user_address_type=address.getString( 
                           address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                  System.out.println("the user address type"+user_address_type);
                  System.out.println(" address might be"+user_home_address);
                contact_attribute_type.add(ADDRESS_TYPE[(Integer.parseInt(user_address_type))-1]);
                contact_attribute_value.add(user_home_address);

                } 
                address.close(); 
            }
            catch(Exception e)
            {
                System.out.println("this exception due to website"+e);
            }

        return(user_home_address);  
        }   


          protected void get_UserWebsite() {
                // TODO Auto-generated method stub
                        try{
                            Cursor websiteNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Website.URL}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                    + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
                                    + "'",null,null);

                            websiteNameCursor.moveToNext();
                            user_website=(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL)));

                            System.out.println("this is my website"+(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL))));
                        contact_attribute_type.add("Website");
                        contact_attribute_value.add(user_website);

                        }
                            catch(Exception e)
                            {
                            user_website=null;
                            System.out.println("this website is"+user_website); 
                            System.out.println("this exception in website"+e);
                            }



            }

                    protected void get_UserOrganization() {
                // TODO Auto-generated method stub
                        try{
                            Cursor organizationNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Organization.COMPANY}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                    + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
                                    + "'",null,null);

                            organizationNameCursor.moveToNext();
                            user_company=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.COMPANY));
                        //    String user_company_type=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TYPE));
                       //   System.out.println("the company type "+user_company_type);
                            System.out.println("this is my organization"+user_company);

                            contact_attribute_type.add("Company");
                        contact_attribute_value.add(user_company);
                        }
                            catch(Exception e)
                            {
                            user_company=null;
                            System.out.println("user company name is"+user_company);
                                System.out.println("this exception in org"+e);
                            }
            }   




    }

By doing this you can get all the fields of contacts-contract.

通过这样做,您可以获得联系人合同的所有字段。

#2


1  

This logs the name and value of all columns in all contact rows. Tested on Galaxy S6 with native contacts app. I needed it to find the column name that is used for custom SMS notification sound (sec_custom_alert).

这会记录所有联系人行中所有列的名称和值。使用原生联系人应用程序在Galaxy S6上测试。我需要它来查找用于自定义SMS通知声音的列名称(sec_custom_alert)。

private void enumerateColumns() {
    Cursor cursor = getApplicationContext().getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            for(int i=0; i<cursor.getColumnCount();i++)
            {
                Log.d("myActivity", cursor.getColumnName(i) + " : " + cursor.getString(i));
            }
        } while (cursor.moveToNext());

        cursor.close();
    }
}