2012-08-05 76 views
0

我已經寫了下面的代碼,以獲得用戶和數量之間的整個談話:通過內容獲取每條短信的電話號碼://短信/

Uri SMS_INBOX = Uri.parse("content://sms/"); 
     String selection = "thread_id = " + thread_id; 
     final String[] projection = new String[] { "*" }; 
     Cursor c = getContentResolver().query(SMS_INBOX, projection, selection,null, "date"); 

     startManagingCursor(c); 

     String[] body = new String[c.getCount()]; 
     String[] address = new String[c.getCount()]; 
     if (c.moveToFirst()) { 
      for (int j = 0; j < c.getColumnCount(); j++) 
       Log.w("ColumnName", c.getColumnName(j)); 
      for (int i = 0; i < c.getCount(); i++) { 
       body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString(); 
       address[i] = c.getString(c.getColumnIndexOrThrow("address")).toString(); 
       Log.d("address-" + i, address[i]); 
       Log.d("body-" + i, body[i]); 
       String subject = c.getString(c.getColumnIndexOrThrow("_id")).toString(); 
       Log.d("_id-" + i, subject); 
       String thread = c.getString(c.getColumnIndexOrThrow("thread_id")).toString(); 
       Log.d("thread_id-" + i, subject); 
       Log.d("----", "----"); 

       c.moveToNext(); 
      } 

     } 

通過這個代碼,我得到的所有對話中的消息。問題是,我找不出哪個號碼正在發送哪條消息。如果我得到列「地址」它一直返回相同的數字(實際上它只返回其他人的數字),所以我不能記錄我剛剛通過此代碼獲得的消息是由用戶發送還是另一個數字。

回答

1

該列將始終只給第二個人號碼。如果您想區分發送的消息和收到的消息,您必須使用'type'列。

body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString(); 

if(c.getString(c.getColumnIndex("type")).equalsIgnoreCase("1")){ 

       // sms received 

       msg_state[i]=1; 

      } 

       else { 
       //sms sent 
       msg_state[i]=0; 

      } 

否您可以輕鬆識別發送的短信和收到的短信。

+0

只要我設法測試這個,我會標記爲答案。謝謝回覆 – 2012-08-31 13:19:07