【问题标题】:Search a contact using phone number使用电话号码搜索联系人
【发布时间】:2012-04-26 12:35:39
【问题描述】:

我想检索与传入消息号码关联的联系人姓名。 我想出了以下代码。

Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender));
        Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME},null,null,null);

        try {
            c.moveToFirst();
            displayName = c.getString(0);
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            c.close();
        }

问题是,它可以在模拟器上运行,但不能在我的移动设备上运行。我尝试重新启动我的手机。但仍然无法正常工作。我搜索了与此类似的代码,但在实际手机上不起作用。

【问题讨论】:

  • 你的阅读手机是什么版本的?

标签: android android-contacts


【解决方案1】:

您需要应用以下代码才能在具有 android 版本 2.1 的 Real Mobile 中工作

    //String msgSender="Mobile No";
    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender));
    Cursor c = getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
    try {
        c.moveToFirst();
     String  displayName = c.getString(0);
    } catch (Exception e) {
        // TODO: handle exception
    }finally{
        c.close();
    }

在 Android 2.1 中,我们必须使用 ContactsContract 并且 PhoneLookup 应该是 ContactsContract

确保您已授予以下权限。

       <uses-permission android:name="android.permission.READ_CONTACTS"/>

【讨论】:

  • 在我的手机中,它正在使用 HTC Incredible S.
猜你喜欢
  • 2011-04-12
  • 2014-11-01
  • 2014-03-30
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
相关资源
最近更新 更多