2013-11-04 59 views
10

獲取短信發件人聯繫人(人)保存的名稱在我的應用程序中,我想使用下面的代碼檢索SMS發件人保存的名稱,但它總是返回空值。請告訴我最簡單的方法來獲取發件人姓名。如何使用「內容://短信/收件箱」

Uri SMS_INBOX = Uri.parse("content://sms/inbox"); 
     Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null); 
     android.util.Log.i("COLUMNS", Arrays.toString(c.getColumnNames())); 

      try { 
       if(c.getCount()>0) 
       { 
        while (c.moveToNext()){ 
        Log.d("SMSss", "Contact : "+c.getString(2)+"\n" 
         +"msg : "+c.getString(11)+"\n" 
         +"ID : "+c.getString(0)+"\n" 
         +"Person : "+c.getString(3)); 
        } 
       } 
      } catch (Exception e) { 

      Log.d("mmmmmmmmm"," "+ e.getStackTrace()); 
      } 

我正在使用menifest

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

以下權限請建議我怎麼去。提前致謝。

+0

名稱menas保存的聯繫人姓名或號碼? –

+0

保存的聯繫人姓名 – IPL10

+0

如果該號碼是未保存在手機中的新號碼,然後?? –

回答

23

通過這種方式,您可以從收件箱中獲取已保存的聯繫人姓名.. 調用方法getAllSms()瞭解詳細內容..

public void getAllSms() { 
    Uri message = Uri.parse("content://sms/"); 
    ContentResolver cr = getContentResolver(); 
    Cursor c = cr.query(message, null, null, null, null); 
    startManagingCursor(c); 
    int totalSMS = c.getCount(); 
    if (c.moveToFirst()) { 
     for (int i = 0; i < totalSMS; i++) { 

      Log.d("SMSss", 
        "Contact number : " 
          + c.getString(c 
            .getColumnIndexOrThrow("address")) 
          + "\n" 
          + "msg : " 
          + c.getColumnIndexOrThrow("body") 
          + "\n" 
          + "ID : " 
          + c.getString(c.getColumnIndexOrThrow("_id")) 
          + "\n" 
          + "Person : " 
          + getContactName(
           getApplicationContext(), 
            c.getString(c 
              .getColumnIndexOrThrow("address")))); 

      c.moveToNext(); 
     } 
    } 
    c.close(); 

} 

public String getContactName(Context context, String phoneNumber) { 
    ContentResolver cr = context.getContentResolver(); 
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(phoneNumber)); 
    Cursor cursor = cr.query(uri, 
      new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    String contactName = null; 
    if (cursor.moveToFirst()) { 
     contactName = cursor.getString(cursor 
       .getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
    } 
    if (cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 
    return contactName; 
} 
+0

感謝@kalyan它的工作 – IPL10

+0

這個過程是如果您想以這種方式檢索所有聯繫人姓名,則花費O(n^2)時間太慢。我正在考慮他們在默認的短信應用中使用了什麼 - 我的猜測是他們正在適配器中檢索它。因此,在我們將它們顯示在列表中之前,不需要獲取所有聯繫人姓名 - 而是僅檢索將要顯示在屏幕上的那些姓名。 – Khobaib

+0

@Khobaib晚會太晚了,但我想我應該發佈一個鏈接到這樣的應用程序。我仍然在努力。名爲[SMSdroid]的應用程序的GitHub鏈接(https://github.com/felixb/smsdroid) – Sufian

3
Uri uriInbox = Uri.parse("content://sms/inbox"); 

      Cursor c = getContentResolver().query(uriInbox, null, null, null, null); 


      if (c.moveToFirst()) { 
       for (int i = 0; i < c.getCount(); i++) { 
        String name = null; 
        String phone = ""; 
        String _id = c.getString(c.getColumnIndexOrThrow("_id")); 
        String thread_id = c.getString(c.getColumnIndexOrThrow("thread_id")); 
        String msg = c.getString(c.getColumnIndexOrThrow("body")); 
        String type = c.getString(c.getColumnIndexOrThrow("type")); 
        String timestamp = c.getString(c.getColumnIndexOrThrow("date")); 
        phone = c.getString(c.getColumnIndexOrThrow("address")); 
        name = Function.getContactbyPhoneNumber(getApplicationContext(), c.getString(c.getColumnIndexOrThrow("address"))); 


        c.moveToNext(); 
       } 
      } 
      c.close(); 

最後你getContactbyPhoneNumber方法:

public String getContactbyPhoneNumber(Context c, String phoneNumber) { 

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME}; 
    Cursor cursor = c.getContentResolver().query(uri, projection, null, null, null); 

    if (cursor == null) { 
     return phoneNumber; 
    }else { 
     String name = phoneNumber; 
     try { 

      if (cursor.moveToFirst()) { 
       name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      } 

     } finally { 
      cursor.close(); 
     } 

     return name; 
    } 
} 

提供:http://www.androstock.com/tutorials/create-sms-app-android-android-studio-part-2.html