正文  设备功能 > 联系人/Contacts >

android学习笔记22--------------通过短信的号码获取联系人

使用前使用后public void readShortMessage() { // 短信数据库查询 ContentResolver cr = ...

使用前
\

使用后
\


public void readShortMessage() 
    { 
        // 短信数据库查询  
        ContentResolver cr = getContentResolver(); 
        Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null, 
                null, null); 
 
        /***************************/ 
        String msg = ""; 
        while (cursor.moveToNext()) 
        { 
            int phoneColumn = cursor.getColumnIndex("address"); 
            int smsColumn = cursor.getColumnIndex("body"); 
 
            msg += cursor.getString(phoneColumn) + ":" 
                    + cursor.getString(smsColumn) + "\n"; 
             
            String contact=getContactNameFromPhoneNum(this.getApplicationContext(),cursor.getString(phoneColumn)); 
             
            getData(contact, cursor.getString(smsColumn)); 
            //getData(cursor.getString(phoneColumn), cursor.getString(smsColumn));  
        } 
    } 
public void readShortMessage()
 {
  // 短信数据库查询
  ContentResolver cr = getContentResolver();
  Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null,
    null, null);

  /***************************/
  String msg = "";
  while (cursor.moveToNext())
  {
   int phoneColumn = cursor.getColumnIndex("address");
   int smsColumn = cursor.getColumnIndex("body");

   msg += cursor.getString(phoneColumn) + ":"
     + cursor.getString(smsColumn) + "\n";
   
   String contact=getContactNameFromPhoneNum(this.getApplicationContext(),cursor.getString(phoneColumn));
   
   getData(contact, cursor.getString(smsColumn));
   //getData(cursor.getString(phoneColumn), cursor.getString(smsColumn));
  }
 }
[java]
public String getContactNameFromPhoneNum(Context context, String phoneNum) 
    { 
        String contactName = ""; 
        ContentResolver cr = context.getContentResolver(); 
        Cursor pCur = cr.query( 
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", 
        new String[]{ phoneNum }, null); 
            if (pCur.moveToFirst()) 
            { 
                contactName = pCur 
                        .getString(pCur 
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
                pCur.close(); 
            } 
        return contactName; 
    } 

摘自 奔跑的蜗牛