2014-06-09 65 views
4

我需要得到的thread_id MMS短信/通話的Android內容提供商的thread_id,這是我迄今所做的:獲取短信/彩信的電話號碼

public long findThreadIdFromPhoneNumber(Context context, PhoneNumber phoneNumber) { 
    Uri.Builder uriBuilder = Uri.withAppendedPath(Uri.parse(CONTENT_SMSMMS+"/"), "threadID").buildUpon(); 
    // phoneNumber.msisdn() return the String phone number 
    uriBuilder.appendQueryParameter("recipient", phoneNumber.msisdn()); 

    long threadId = -1L; 
    Cursor cursor = null; 
    try { 
     cursor = context.getContentResolver().query(
       uriBuilder.build(), 
       new String[] { Contacts._ID }, 
       null, null, null); 
     if (cursor != null && cursor.moveToFirst()) { 
      threadId = cursor.getLong(0); 
     } 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
    return threadId; 
} 

的問題是,此代碼在內容提供者中創建新線程,並且我不需要這些,如果對話存在,我只需要返回thread_id,如果不存在則返回-1。

我也試過這個代碼:

public long findThreadIdFromPhoneNumber(Context context, PhoneNumber phoneNumber) { 
    long threadId = -1L; 
    Cursor cursor = null; 
    try { 
     if (context==null || context.getContentResolver()==null || phoneNumber==null || phoneNumber.msisdn()==null) 
      return threadId; 
     cursor = context.getContentResolver().query(
      Uri.parse(CONTENT_SMSMMS_CONVERSATIONS), 
      // phoneNumber.msisdn() return the String phone number 
      null, "address='"+phoneNumber.msisdn()+"' ", 
      null, null 
     ); 
     if (cursor != null && cursor.moveToFirst()) { 
      if (cursor.getColumnIndex("thread_id")>=0) 
       threadId = cursor.getLong(cursor.getColumnIndex("thread_id")); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     String number = (phoneNumber!=null && phoneNumber.msisdn()!=null) ? phoneNumber.msisdn() : ""; 
     Logcat.e("Error during findThreadIdFromPhoneNumber for "+number+": "+e.getMessage(), e); 
     return threadId; 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
    return threadId; 
} 

但這個代碼給我一個外部NullPointerException在某些手機上ContentResolver,和它的作品在其他:

java.lang.NullPointerException 
at android.os.Parcel.readException(Parcel.java:1437) 
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185) 
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137) 
at android.content.ContentProviderProxy.query(ContentProviderNative.java:385) 
at android.content.ContentResolver.query(ContentResolver.java:417) 
at android.content.ContentResolver.query(ContentResolver.java:360) 
at com.mypackage.android.sms.SmsMmsManager.findThreadIdFromPhoneNumber(SmsMmsManager.java:115) 

回答

1

使用這個URI用於獲取

ContentResolver cr = context.getContentResolver(); 
    Cursor pCur = cr.query(
      Uri.parse("content://mms-sms/canonical-addresses"), new String[]{"_id"}, 
      "address" + " = ?", 
      new String[]{your_address}, null); 

    String thread_id = null; 

    if (pCur != null) { 
     if (pCur.getCount() != 0) { 
      pCur.moveToNext(); 
      thread_id = pCur.getString(pCur.getColumnIndex("_id")); 
     } 
     pCur.close(); 
    } 
1
public MessageObjects getSms2(Context context , String number) { 

    ContentResolver contentResolver = context.getContentResolver(); 
    Uri uri = Uri.parse("content://sms/"); 

    Cursor cursor = contentResolver.query(uri, null, "thread_id IS NOT NULL) GROUP BY (thread_id AND address=?", new String[]{number}, "date DESC"); 

    String displayName; 
    String formattedDate = ""; 
    String photoUri = null; 
    MessageObjects contct = new MessageObjects(); 
    while (cursor.moveToNext()) { 
     displayName = ""; 
     long key = cursor.getLong(cursor.getColumnIndex("_id")); 
     long threadId = cursor.getLong(cursor.getColumnIndex("thread_id")); 
     String address = cursor.getString(cursor.getColumnIndex("address")); // phone # 
     long date = cursor.getLong(cursor.getColumnIndex("date")); 
     Date callDayTime = new Date(Long.valueOf(date)); 
     String body = cursor.getString(cursor.getColumnIndex("body")); 
     String person = cursor.getString(cursor.getColumnIndex("person")); 

     contct.setThreadId(threadId); 
     contct.setNumber(address); 

     contct.setTime(date); 
     contct.setMsg(body); 
     contct.setPerson(displayName); 


    } 
    cursor.close(); 
    return contct; 
} 
相關問題