android通过号码查询联系人姓名

复制就可以用。传入号码和上下文

    public static String queryNameByNum(String num,Context context)
    {
        Cursor cursorOriginal =
            context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+num+"'",null,null);
        if(null!=cursorOriginal)
        {
            if(cursorOriginal.getCount()>1)
            {
                return null;
            }else{
                if(cursorOriginal.moveToFirst())
                {
                    return cursorOriginal.getString(cursorOriginal.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                }else
                {
                    return null;
                }
            }
        }else
        {
            return null;
        }
    }

你可能感兴趣的:(android,应用,游戏基础)