检查传入号码是存储在联系人列表中还是不存在于android中

检查传入号码是存储在联系人列表中还是不存在于android中,第1张

概述在我的Android应用程序,当一个来电我想显示我的自定义ui,我能够做到这一点.不,我想检查收到的号码是否来自联系人.下面是我执行此 *** 作的代码,但它为存储在我的联系人列表中的传入号码返回null.publicStringfindNameByNumber(Stringnum){Uriuri=Uri.withAppendedPath(

在我的Android应用程序,当一个来电我想显示我的自定义ui,我能够做到这一点.
不,我想检查收到的号码是否来自联系人.
下面是我执行此 *** 作的代码,但它为存储在我的联系人列表中的传入号码返回null.

public String findnameByNumber(String num){    Uri uri = Uri.withAppendedpath(Phones.CONTENT_FILTER_URL, Uri.encode(num));    String name = null;    Cursor cursor = mContext.getContentResolver().query(uri,                         new String[] { Phones.disPLAY_name }, null, null, null);    if (cursor != null && cursor.movetoFirst()) {        name = cursor.getString(cursor.getColumnIndex(Phones.disPLAY_name));        cursor.close();        callyname.setText(name+" Calling..");    }    return name;}

我有来自一个号码917878787878的来电,但在我的联系人中,这个联系人存储为名称XYZ,编号为78 78 787878,由于数字之间存在空格而形成,并且还尝试排除91但仍然返回null.
那么我怎样才能找到以任何格式存储的号码.哪些号码可以与国家代码一起存储.

提前致谢.

解决方法:

请尝试使用此代码(使用PhoneLookup.CONTENT_FILTER_URI而不是电话):

String res = null;    try {        ContentResolver resolver = ctx.getContentResolver();        Uri uri = Uri.withAppendedpath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));        Cursor c = resolver.query(uri, new String[]{PhoneLookup.disPLAY_name}, null, null, null);        if (c != null) { // cursor not null means number is found contactstable            if (c.movetoFirst()) {   // so Now find the contact name                res = c.getString(c.getColumnIndex(CommonDataKinds.Phone.disPLAY_name));            }            c.close();        }    } catch (Exception ex) {        /* Ignore */    }            return res;

正如文档所述ContactsContract.PhoneLookup:表示查找电话号码的结果的表格,例如来电者ID.要执行查找,您必须将要查找的数字附加到CONTENT_FILTER_URI.此查询已高度优化.

总结

以上是内存溢出为你收集整理的检查传入号码是存储在联系人列表中还是不存在于android中全部内容,希望文章能够帮你解决检查传入号码是存储在联系人列表中还是不存在于android中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096239.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存