Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检索联系信息时出现问题_Android - Fatal编程技术网

Android 检索联系信息时出现问题

Android 检索联系信息时出现问题,android,Android,我想从一个号码中获取联系人姓名。我曾尝试通过查询检索该号码,但没有得到结果。它正在返回号码本身。我已将该号码保存到联系人 我试过的代码是 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.acti

我想从一个号码中获取联系人姓名。我曾尝试通过查询检索该号码,但没有得到结果。它正在返回号码本身。我已将该号码保存到联系人

我试过的代码是

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String x = getContactNameFromNumber("+918281306132");
    System.out.println(x);
}
private String getContactNameFromNumber(String number) {
    // define the columns I want the query to return

    System.out.println("Entering into getContactNameFromNumber");
    String[] projection = new String[] {
            Contacts.Phones.DISPLAY_NAME,
            Contacts.Phones.NUMBER };

    // encode the phone number and build the filter URI
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

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

    // if the query returns 1 or more results
    // return the first result
    if (c.moveToFirst()) {
        String name = c.getString(c
                .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
        return name;
    }

    // return the original number if no match was found
    return number;
}
}

我还添加了一个读取电话状态许可。有人请帮我检索联系人姓名

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

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

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    // Query phone here. Covered next

                }
            }
这个代码对我有用


在代码中执行以下更改

Uri contactUri = Uri.withAppendedPath(Phone.CONTENT_URI, Uri.encode(number));


试试看,希望对你有帮助。:)

您可以尝试我的代码片段:

public static String findContactByNumber(String phoneNumber,
        ContentResolver cr) {

    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));

    String[] phoneNoProjections = { PhoneLookup._ID,
            PhoneLookup.DISPLAY_NAME };

    Cursor cursor = cr.query(lookupUri, phoneNoProjections, null, null,
            null);

    try {

        if (cursor.moveToFirst()) {

            return cursor.getString(1); //1 is the display name index. 0 is id.

        }

    } finally {

        if (cursor != null)

            cursor.close();

    }

    return null;

}

你的代码看起来不错。您可以尝试更改
String name=c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_name))
to
String name=c.getString(0)。或者你可以试试我的。如果有效,我会添加答案。我尝试过更改代码,但仍然无效。你在logcat中看到任何异常吗?没有,它返回的号码本身不是联系人姓名好的,我得到了ans。你可以发布ans,以便我可以将其标记为正确的ans,谢谢
public static String findContactByNumber(String phoneNumber,
        ContentResolver cr) {

    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));

    String[] phoneNoProjections = { PhoneLookup._ID,
            PhoneLookup.DISPLAY_NAME };

    Cursor cursor = cr.query(lookupUri, phoneNoProjections, null, null,
            null);

    try {

        if (cursor.moveToFirst()) {

            return cursor.getString(1); //1 is the display name index. 0 is id.

        }

    } finally {

        if (cursor != null)

            cursor.close();

    }

    return null;

}