2

我知道如何在 Android 上获取所有联系人,但如果我选择联系人,我无法获得我想要的联系人。示例:我有 4 个联系人

joe have phone number 7889 987;
erick have phone number 8792 871;
nona phone number 3653 872 and 2345 907;
rina phone number 8796 235;

如果我选择 joe,我会得到 nona 的电话号码 = 2345 907 我不知道我的应用程序有什么问题。

这是我的代码

public void tambahPenerima ( View v ) {
    Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    i.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(i, PICK_CONTACT);    
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    if(resultCode == RESULT_OK) {
            Cursor cursor = null;
            String name = "";
            String number = "";

            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            while(cursor.moveToNext()) {
                name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } 
                if(cursor!=null) {
                    cursor.close();
                }

                Intent kirimPesan = new Intent();
                kirimPesan.setClass(this, kirimPesan.class);
                kirimPesan.putExtra("nama", name);
                kirimPesan.putExtra("nomor", number);
                kirimPesan.putExtra("chiper", chiper);
                startActivity(kirimPesan);
    }                   
}

有人请帮助我,我真的需要帮助。对不起我糟糕的英语。谢谢 ..

4

2 回答 2

0
     cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
 if(cursor != null && cursor.moveToFirst()) {

                while(cursor.moveToNext()) {
                    name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                    number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                } 
}
于 2013-03-12T05:14:43.907 回答
0

这可以帮助您获得具体的联系方式

在这里姓名或号码通过并获取您想要的联系方式

 private String getContactName(Context context, String number) {
        String name = null;
        // define the columns I want the query to return
        String[] projection = new String[] {
                ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.PhoneLookup._ID};

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        // query time
        Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

        if(cursor != null) {
            if (cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                Log.v(TAG, "Started uploadcontactphoto: Contact Found @ " + number);            
                Log.v(TAG, "Started uploadcontactphoto: Contact name  = " + name);
            } else {
                Log.v(TAG, "Contact Not Found @ " + number);
            }
            cursor.close();
        }
        return name;
    }
于 2013-03-12T06:07:24.940 回答