• 在android中读取联系人信息的程序,包括读取联系人姓名、*和邮箱


     1:androidmanifest.xml的内容

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="cn.itcast.contacts"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
         <uses-library android:name="android.test.runner" />
            <activity android:name=".MainActivity"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
        <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.WRITE_CONTACTS" />
        
         <instrumentation android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" />
    </manifest> 

    里面重要的是搭建测试环境和添加<uses-permission>,即读取和写入联系人信息的权限

    2:读取的主要方法

    /*
         * 读取联系人的信息
         */
        public void testReadAllContacts() {
            Cursor cursor = this.getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
                     null, null, null, null);
            int contactIdIndex = 0;
            int nameIndex = 0;
            
            if(cursor.getCount() > 0) {
                contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            }
            while(cursor.moveToNext()) {
                String contactId = cursor.getString(contactIdIndex);
                String name = cursor.getString(nameIndex);
                Log.i(TAG, contactId);
                Log.i(TAG, name);
                
                /*
                 * 查找该联系人的phone信息
                 */
                Cursor phones = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, 
                        null, null);
                int phoneIndex = 0;
                if(phones.getCount() > 0) {
                    phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                }
                while(phones.moveToNext()) {
                    String phoneNumber = phones.getString(phoneIndex);
                    Log.i(TAG, phoneNumber);
                }
                
                /*
                 * 查找该联系人的email信息
                 */
                Cursor emails = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                        null, 
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, 
                        null, null);
                int emailIndex = 0;
                if(emails.getCount() > 0) {
                    emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
                }
                while(emails.moveToNext()) {
                    String email = emails.getString(emailIndex);
                    Log.i(TAG, email);
                }
                
            }
        }

    3:目前手机中的联系人信息,有两个联系人的信息,如图所示,

    这是Hellen的联系信息

    这是Mike的联系信息

    4:测试结果,在控制台输出的内容为:

    可以看到在控制台输出了两个联系人的信息。

    //--------------------------------------

    获取所有分组编号和组的名称

    Cursor groupInfoCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, null);
    while(groupInfoCursor.moveToNext()) {
        int idIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups._ID);
        int titleIndex = groupInfoCursor.getColumnIndex(ContactsContract.Groups.TITLE);
        String id = groupInfoCursor.getString(idIndex);
        String title = groupInfoCursor.getString(titleIndex);
    }
    groupInfoCursor.close();
  • 相关阅读:
    Linux 的文件软链接如何删除
    mysql-xtrabackup备份sh: xtrabackup_56: command not found与error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory
    How To Upgrade ASMLib Kernel Driver as Part of Kernel Upgrade? (文档 ID 1391807.1)
    [trouble] error connecting to master 'repl@192.168.1.107:3306'
    Troubleshooting 10g and 11.1 Clusterware Reboots (文档 ID 265769.1)
    "Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs
    RMAN-06900 RMAN-06901 ORA-19921
    一则ORACLE进程都在但是无法进入实例的问题
    VirtualBox下Win7下CPU高占用的一次故障解决
    netcore之mysql中文乱码问题解决记录
  • 原文地址:https://www.cnblogs.com/error404/p/2956090.html
Copyright © 2020-2023  润新知