Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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 static String getPhone(Context context, String displayName) { ContentResolver cr = context.getContentResolver(); Uri uri = CommonDataKinds.Phone.CONTENT_URI; String selection = CommonDa

我试图通过提供电话号码从联系人处获取电话号码。到目前为止我所做的:

public static String getPhone(Context context, String displayName) {
        ContentResolver cr = context.getContentResolver();
        Uri uri = CommonDataKinds.Phone.CONTENT_URI;
        String selection = CommonDataKinds.Phone.DISPLAY_NAME+" LIKE '%" + displayName + "&'";
        Cursor cursor = cr.query(uri, new String[]{CommonDataKinds.Phone._ID,CommonDataKinds.Phone.DISPLAY_NAME,CommonDataKinds.Phone.NUMBER}, selection, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if(cursor.moveToFirst()) {
            contactName = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER));

        }

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        //Log.d("Contact",contactName.toString());
        return contactName;
    }

请尝试此代码。您需要在清单“android.permission.READ_CONTACTS”中设置true上的权限

更新:

此代码段将允许您读取特定联系人的所有消息:

StringBuilder smsBuilder = new StringBuilder();
       final String SMS_URI_INBOX = "content://sms/inbox"; 
        final String SMS_URI_ALL = "content://sms/";  
        try {  
            Uri uri = Uri.parse(SMS_URI_INBOX);  
            String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };  
            Cursor cur = getContentResolver().query(uri, projection, "address='123456789'", null, "date desc");
             if (cur.moveToFirst()) {  
                int index_Address = cur.getColumnIndex("address");  
                int index_Person = cur.getColumnIndex("person");  
                int index_Body = cur.getColumnIndex("body");  
                int index_Date = cur.getColumnIndex("date");  
                int index_Type = cur.getColumnIndex("type");         
                do {  
                    String strAddress = cur.getString(index_Address);  
                    int intPerson = cur.getInt(index_Person);  
                    String strbody = cur.getString(index_Body);  
                    long longDate = cur.getLong(index_Date);  
                    int int_Type = cur.getInt(index_Type);  

                    smsBuilder.append("[ ");  
                    smsBuilder.append(strAddress + ", ");  
                    smsBuilder.append(intPerson + ", ");  
                    smsBuilder.append(strbody + ", ");  
                    smsBuilder.append(longDate + ", ");  
                    smsBuilder.append(int_Type);  
                    smsBuilder.append(" ]\n\n");  
                } while (cur.moveToNext());  

                if (!cur.isClosed()) {  
                    cur.close();  
                    cur = null;  
                }  
            } else {  
                smsBuilder.append("no result!");  
            } // end if  
            }
        } catch (SQLiteException ex) {  
            Log.d("SQLiteException", ex.getMessage());  
        } 

请尝试此代码。您需要在清单“android.permission.READ_CONTACTS”中设置true上的权限

更新:

此代码段将允许您读取特定联系人的所有消息:

StringBuilder smsBuilder = new StringBuilder();
       final String SMS_URI_INBOX = "content://sms/inbox"; 
        final String SMS_URI_ALL = "content://sms/";  
        try {  
            Uri uri = Uri.parse(SMS_URI_INBOX);  
            String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };  
            Cursor cur = getContentResolver().query(uri, projection, "address='123456789'", null, "date desc");
             if (cur.moveToFirst()) {  
                int index_Address = cur.getColumnIndex("address");  
                int index_Person = cur.getColumnIndex("person");  
                int index_Body = cur.getColumnIndex("body");  
                int index_Date = cur.getColumnIndex("date");  
                int index_Type = cur.getColumnIndex("type");         
                do {  
                    String strAddress = cur.getString(index_Address);  
                    int intPerson = cur.getInt(index_Person);  
                    String strbody = cur.getString(index_Body);  
                    long longDate = cur.getLong(index_Date);  
                    int int_Type = cur.getInt(index_Type);  

                    smsBuilder.append("[ ");  
                    smsBuilder.append(strAddress + ", ");  
                    smsBuilder.append(intPerson + ", ");  
                    smsBuilder.append(strbody + ", ");  
                    smsBuilder.append(longDate + ", ");  
                    smsBuilder.append(int_Type);  
                    smsBuilder.append(" ]\n\n");  
                } while (cur.moveToNext());  

                if (!cur.isClosed()) {  
                    cur.close();  
                    cur = null;  
                }  
            } else {  
                smsBuilder.append("no result!");  
            } // end if  
            }
        } catch (SQLiteException ex) {  
            Log.d("SQLiteException", ex.getMessage());  
        } 

还有什么不起作用/不像你期望的那样?您请求帮助的问题是什么?
通过提供电话号码获取电话号码(…)
?还有什么不起作用/不符合您的预期?您请求帮助的问题是什么?
通过提供电话号码来获取电话号码(…)
?这很有效,但当我将号码应用到sms收件箱以从当前号码获取消息时,它不会返回任何信息…我更新了我的帖子。如果它有效,你能把这个标记为正确答案吗?这是可行的,但当我将数字buck应用到sms收件箱以从当前数字获取消息时,它不会返回任何信息…我更新了我的帖子。如果它有效,你能把这个标记为正确答案吗?谢谢