2012-07-02 157 views
1

如何搜索短信收件箱並顯示來自特定號碼的最新消息。例如,搜索「999999999」並顯示從該號碼收到的最後一條消息。有沒有辦法做到這一點?搜索短信收件箱

我使用此代碼返回的郵件數我的收件箱

TextView view; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    view = (TextView) findViewById(R.id.textView1); 

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); 
    Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null); 
    int unreadMessagesCount = c.getCount(); 
    c.deactivate(); 

    view.setText(String.valueOf(unreadMessagesCount)); 

回答

6

如果您只需要通過一個特定號碼使用發送的最後一條消息是:

final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); 
Cursor cursor = getContentResolver().query(SMS_INBOX, null, "address = ?", 
     new String[] {"9999999999"}, "date desc limit 1"); 
if(cursor.moveToFirst()) { 
    // Do something 
    Log.v("Example", cursor.getString(cursor.getColumnIndex("body"))); 
} 
cursor.close(); 

How many database columns associated with a SMS in android?的第二個答案是SMS中不同列的很好的參考。

1

嘗試爲:

final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); 
Cursor c = getContentResolver().query(SMS_INBOX,null,null, null,"DATE desc"); 
if(c.moveToFirst()){ 
     for(int i=0;i<c.getCount();i++){ 
      String body= c.getString(c.getColumnIndexOrThrow("body")).toString(); 
      String number=c.getString(c.getColumnIndexOrThrow("address")).toString(); 
      if(number=="999999999") 
      { 
      //get message body here 
        break; 
      } 
      else 
      c.moveToNext(); 
     } 
    } 
c.close();