从PhoneBook获取联系号码在android中

时间:2015-05-09 08:20:29

标签: android android-activity android-contacts android-logcat android-cursor

我正在开发一个简单的应用,当我点击 EditText 时,电话联系将会打开,当用户选择一个联系人时,它会显示在EditText上。 我已经完成了,但问题是某些联系人显示输出如下:

05-09 13:39:03.660: D/Actual No--->>>(953): 84 85 835654
05-09 13:39:03.660: D/phoneno(953): Tufan 85 835654
05-09 13:39:08.911: W/IInputConnectionWrapper(953): showStatusIcon on inactive InputConnection
05-09 13:39:15.428: D/Actual No--->>>(953): 08485835654
05-09 13:39:15.428: D/phoneno(953): Tufan8485835654
05-09 13:39:22.512: W/IInputConnectionWrapper(953): showStatusIcon on    inactive InputConnection
05-09 13:39:31.825: W/IInputConnectionWrapper(953): showStatusIcon on inactive InputConnection
05-09 13:39:42.468: W/IInputConnectionWrapper(953): showStatusIcon on inactive InputConnection
05-09 13:45:16.147: D/Actual No--->>>(953): +918485835654
05-09 13:45:16.147: D/phoneno(953): Tufan8485835654

因此,您可以看到一些联系,它给了我正确的结果。怎么解决这个朋友?

检查我的java代码:

Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if (hasPhone.equalsIgnoreCase("1")) {
        Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                null, null);
        phones.moveToFirst();
        String phn_no = phones.getString(phones.getColumnIndex("data1"));
        Log.d("Actual No--->>>", phn_no);
        int len = phn_no.length() % 10;
        String phn_no1 = phn_no.substring(len, phn_no.length());
        Log.d("phoneno", "Tufan" + phn_no1);
    }
}

我认为问题在于数字之间的空间,但它取决于用户如何保存。但作为程序员,我需要解决。如果有人知道,请帮助我。

1 个答案:

答案 0 :(得分:0)

尝试使用此代码运行代码。

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG)
                .show();

        System.out.println(name + "==" + phoneNumber);

    }
    phones.close();