2011-08-08 135 views
0

我使用下列以獲得從收件箱中的短信從短信表中獲取發件人的聯繫人姓名?

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(Uri.parse("content://sms"), null,null,null, null);   
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 

    cursor_count=cur.getCount(); 

    if(cur.moveToFirst()) 
    {   
     do { 

      sender.add(cur.getString(2)); 

      msg.add(cur.getString(11)); 
      date.add(formatter.format(Long.parseLong(cur.getString(4)))); 
     } while (cur.moveToNext()); 

我從地址欄的人的地址,但我也想發送的聯繫人姓名,如果儲存在我的聯繫人。
人字段總是返回null,爲什麼?

回答

6

試試這個代碼

String contact=address; 
          Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)); 
          Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null); 

          if(cs.getCount()>0) 
          { 
          cs.moveToFirst(); 
          contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
          } 
+0

無法導入PhoneLookup? – Harinder

+0

actully我使用1.6,但我們的權利2.0以上的情況下 – Harinder

+1

什麼是地址? – SpicyWeenie

相關問題