通过联系人姓名获取电话号码

DimitriosP

我正在尝试通过提供电话号码来从联系人获取电话号码。到目前为止,我做了什么:

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。

public String getPhoneNumber(String name, Context context) {
    String ret = null;
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
    Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, selection, null, null);
    if (c.moveToFirst()) {
        ret = c.getString(0);
    }
    c.close();
    if(ret==null)
        ret = "Unsaved";
    return ret;
    }

更新:

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

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());  
        } 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从电话号码获取实际的联系人姓名

如何通过姓名和电话号码搜索联系人?

在Android中使用电话号码获取联系人姓名

在Swift中获取联系人姓名和电话号码

如何从他的电话号码中获取联系人的姓名

如何获取给定电话号码的联系人姓名

Android如何通过我的通话记录联系人电话号码获取联系人ID

使用ContentProvider从联系人获取电话号码-Android

从Android联系人簿获取电话号码

Android-从电话号码获取联系人照片

Android从联系人列表中获取电话号码

从联系人列表中获取电话号码

我如何获取所选联系人的电话号码

iOS通讯录如何通过电话号码获取联系人

以编程方式更新android中的联系人姓名和电话号码

在javascript中通过google登录获取所有用户的联系人列表(包括电话号码)

通过电话号码搜索联系人,并使用3位数前缀过滤

通过电话号码从Exchange Server查询GAL联系人

如何选择联系人的非默认电话号码?

如何从联系人到我的SMS应用程序获取电话号码

Android在ArrayList中获取所有联系人的电话号码

快速获取带有ContactsUI的所有联系人电话号码

从ListView中选择联系人,并在Android中获取他们的电话号码

如何获取所需的联系人显示名称基于反应本机中的电话号码

从联系人Android获取一个电话号码

搜索联系人并从android中的电话联系人获取联系人号码

使用联系人Android Studio的电话号码在联系人列表中打开联系人信息

如何通过Xamarin C#在Android中添加具有多个电话号码的联系人?

在ngCordova Contacts插件中获取所有联系人至少一个电话号码