2011-11-21 70 views
0

我正在使用此代码。检索设备的所有收件箱中的消息android

Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); 

SmsMessage[] messages = new SmsMessage[pduArray.length]; 

StringBuilder messageText = new StringBuilder(); 

for (int i = 0; i < pduArray.length; i++) {     
    messages[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);    
    messageText.append(messages[i].getMessageBody());   
} 

但这种代码是唯一的亲密当在设备上接收短信,但我需要的是找回被保存在安卓Android设备我该怎么做到这一切的收件箱中的邮件?

+0

检查:http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in -Android。 – Ian

+0

选中此项:[Android - 获取收件箱短信](http://www.technotalkative.com/?p=1817) –

回答

5

试试这个代码:

Uri SMSURI = Uri.parse("content://sms/inbox"); 
    String[] projection = new String[]{"_id", "address", "body", "date"}; 
    Cursor cursor = null; 

    try { 
     cursor = getContentResolver().query(SMSURI 
       , projection 
       , null //selection 
       , null //selectionArgs 
       , null); //sortOrder 

     if (cursor != null && cursor.moveToFirst()) { 
      do { 
       int id = cursor.getInt(cursor.getColumnIndex("_id")); 
       String address = cursor.getString(cursor.getColumnIndex("address")); 
       String body = cursor.getString(cursor.getColumnIndex("body")); 
       String date = cursor.getString(cursor.getColumnIndex("date")); 

       Toast.makeText(getBaseContext(), "Message is >>"+body, 
          Toast.LENGTH_SHORT).show(); 
       } 

      } while (cursor.moveToNext()); 
     } 

    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
相关问题