在Android的2.3.4获取联系人的名字从数 [英] Getting contact name from number in Android 2.3.4

查看:147
本文介绍了在Android的2.3.4获取联系人的名字从数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在2.3.4的Andr​​oid使用一些联系人的姓名,但它是行不通的。这里我重视我的code。请帮助..我已经试了很多方法如过流堆栈贴在模拟器其工作,但失败而在手机的运行速度。

 的String [] =投影新的String [] {Contacts.Phones.DISPLAY_NAME,
                                     Contacts.Phones.NUMBER};// EN code中的电话号码,并建立过滤器URI
Toast.makeText(背景下,发件人:+发件人,Toast.LENGTH_LONG).show();乌里contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
                                      Uri.en code(发件人));//查询时间
光标C = context.getContentResolver()查询(contactUri,投影,空,
                                              NULL,NULL);//如果查询返回1或更多结果
//返回的第一个结果
如果(c.getCount()大于0){
    如果(c.moveToFirst()){
        名称= c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
    }
}其他{
    NAME =未知;
}


解决方案

综观API来的 Contacts.Phones.NUMBER


  

公共静态最后弦乐的 NUMBER


  
  

的电话号码作为用户输入它。


所以必须指定你在程序中使用数字的完全相同的(每个字符)作为一个在电话簿。这可能是为什么它在手机上的失败,你的电话本中可能包含 + 46xxxxxxxx 国家code的信息。

要解决这个问题,请使用 PhoneLookup ContactsContract 它将使用一种算法来检查数是相等的(也
Contacts.Phones 的常数德precated):

 公共静态字符串getContactName(字符串NUM,ContentResolver的CR){    乌里U = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI Uri.en code(NUM));
    的String [] =投影新的String [] {} ContactsContract.Contacts.DISPLAY_NAME;    光标C = cr.query(U,投影,NULL,NULL,NULL);    尝试{
        如果(!c.moveToFirst())
            返回数;        INT指数= c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        返回c.getString(索引);    } {最后
        如果(C!= NULL)
            c.close();
    }
}

(这code返回,如果没有接触发现与数目。)

i try to get the name of contact using number in 2.3.4 android, but it was not working.. here i have attached my code. Kindly assist.. i have tried so many ways as posted in Stack over flow, in emulator its working but fails while runs in phone..

String[] projection = new String[] { Contacts.Phones.DISPLAY_NAME, 
                                     Contacts.Phones.NUMBER };

// encode the phone number and build the filter URI
Toast.makeText(context, "sender: "+sender, Toast.LENGTH_LONG).show();

Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
                                      Uri.encode(sender));

// query time
Cursor c = context.getContentResolver().query(contactUri, projection, null,
                                              null, null);

// if the query returns 1 or more results
// return the first result
if(c.getCount()>0){
    if (c.moveToFirst()) {
        name = c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
    }                
}else{
    name="UnKnown";
}

解决方案

Looking at the API for Contacts.Phones.NUMBER:

public static final String NUMBER

The phone number as the user entered it.

So the number you use in the program must be specified exactly the same (character by character) as the one in the phone book. This might be why it fails on the phone as your phone book might contain country code information like +46xxxxxxxx.

To get around this issue use PhoneLookup from ContactsContract it will use an algorithm to check if the numbers are equal (also, the constants from Contacts.Phones are deprecated):

public static String getContactName(String num, ContentResolver cr) {

    Uri u = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI Uri.encode(num));
    String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME};

    Cursor c = cr.query(u, projection, null, null, null);

    try {
        if (!c.moveToFirst())
            return number;

        int index = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        return c.getString(index);

    } finally {
        if (c != null)
            c.close();
    }
}

(This code returns the number if no contact is found with that number.)

这篇关于在Android的2.3.4获取联系人的名字从数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆