获取所有android联系人和详细信息,并放入单个多维数组

时间:2014-02-24 16:36:09

标签: java android arrays multidimensional-array

是Android(Java)的新手,我想执行一项简单的任务 我如何获取设备上的所有联系人电话号码,电子邮件和全名以及他们的详细信息并放入 它们就像一个阵列一样 我来自PHP背景,所以这个阵列会在PHP中看起来像这样:

 ContactsArray[0] = array(
                    'fullname' => 'John doe',
                    'Phone' => '0909809890' ,
                    'email' => 'johndoe@email.com'
                         )

从PHP的角度来看,数组应该是什么样的, 我是Android新手,想要一个类来获取所有这些数据 把它们放在一个数组中 返回数据。

PSEUDO CODE

     function GetContacts(){
     contacts = fetchAllContactsDetails
     return contacts;
     }

2 个答案:

答案 0 :(得分:4)

这可能是一个开始。请注意,我没有对此进行测试,可能会出现错误,但您会明白这一点。

阅读本文以获取有关多维数组的更多信息。 Link

String phpArray;
int x=3;
int y=array.length();
String[][] myStringArray = new String [y][x];

for(int i=0; i<array.length; i++){
    for(int j=0; j<3; j++){
        phpArray = contactsArray[i];
        myStringARray[i][j] = phpArray[j];
    }

}

答案 1 :(得分:1)

首先,您需要添加读取清单文件中联系人的权限:

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

您可以查看此代码:

public void fetchContacts() { 
    String phoneNumber = null; 
    String email = null; 
    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
    String _ID = ContactsContract.Contacts._ID; 
    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 
    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 
    Uri EmailCONTENT_URI =  ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID; 
    String DATA = ContactsContract.CommonDataKinds.Email.DATA; 
    StringBuffer output = new StringBuffer(); 
    ContentResolver contentResolver = getContentResolver(); 
    Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);   
    // Loop for every contact in the phone 
    if (cursor.getCount() > 0) { 
        while (cursor.moveToNext()) { 
            String contact_id = cursor.getString(cursor.getColumnIndex( _ID )); 
            String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME )); 
            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER ))); 
            if (hasPhoneNumber > 0) { 
                output.append("\n First Name:" + name); 
                // Query and loop for every phone number of the contact 
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null); 
                while (phoneCursor.moveToNext()) { 
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
                    output.append("\n Phone number:" + phoneNumber); 

                } 
                phoneCursor.close(); 
                // Query and loop for every email of the contact 
                Cursor emailCursor = contentResolver.query(EmailCONTENT_URI,    null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null); 

                while (emailCursor.moveToNext()) { 

                    email = emailCursor.getString(emailCursor.getColumnIndex(DATA)); 
                    output.append("\nEmail:" + email); 

                } 

                emailCursor.close(); 
            } 
            output.append("\n"); 

        } 
        outputText.setText(output); 
    } 
} 

}