2013-02-06 33 views
2

我想知道,如何以編程方式讀取特定號碼的短消息? 我知道如何使用內容提供商,但不知道我是否應該使用「人」欄或「地址」欄或完全不同的方式來讀取短信 請幫 感謝獲取特定電話號碼的短信

回答

5

它會列出消息從指定的數。

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox"); 
     Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null); 
     startManagingCursor(cursor1); 
     String[] columns = new String[] { "address", "person", "date", "body","type" }; 
     if (cursor1.getCount() > 0) { 
      String count = Integer.toString(cursor1.getCount()); 
      while (cursor1.moveToNext()){ 
       String address = cursor1.getString(cursor1.getColumnIndex(columns[0])); 

       if(address.equalsIgnoreCase("number")){ //put your number here 
        String name = cursor1.getString(cursor1.getColumnIndex(columns[1])); 
        String date = cursor1.getString(cursor1.getColumnIndex(columns[2])); 
        String body = cursor1.getString(cursor1.getColumnIndex(columns[3])); 
        String type = cursor1.getString(cursor1.getColumnIndex(columns[4])); 

        Log.d("*******", "body="+body); 

       } 


      } 
     } 

但我遇到"content://mms-sms/conversations/"我認爲這將返回的幫助thread_id具體數量直接整個對話來了,檢查this

+0

你好,感謝您的答案,但這並不回答我的問題。在這裏,你在說我如何準備好收到的短信。我的問題是,我有一個特定的號碼「xxxxxxxx」,我怎樣才能從收件箱中獲得這個號碼發送的所有消息。所以沒有什麼與onReceive相關,因爲它是一個閱讀收件箱短信並通過它們嘗試查找特定電話號碼的短信的活動。 – Snake

+0

我聞你有雙卡,現在你想要獲取所有發送的信息具體的號碼,但這句話並不清楚,你怎麼才能從收件箱中獲得這個號碼發送的所有消息。由於收件箱包含收到的郵件未發送,並且您希望從收件箱發送郵件,因此收件箱沒有任何意義。 – RobinHood

+0

我不是指「已發送」消息。我指的是「收到的消息」。如果你仔細閱讀我的評論,你會發現我問的是發送「BY」這個號碼的消息不是「To」。換句話說,我在尋找從這個號碼收到的消息。我不想收聽目前收到的消息,而是收聽過去收到的消息。例如,我想知道昨天從號碼xxxx收到的消息。 – Snake

0
public class SmsReceiver extends BroadcastReceiver { 

    String specificPhoneNumber = "No you want"; 

    public void onReceive(Context context, Intent intent) 
    { 
      //---get the SMS message passed in--- 
      Bundle bundle = intent.getExtras();   
      SmsMessage[] msgs = null; 
      String str = ""; 

      if (bundle != null) 
      {   
       //---retrieve the SMS message received--- 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       msgs = new SmsMessage[pdus.length];    
       for (int i=0; i<msgs.length; i++){ 
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
        String phNum = msgs[i].getOriginatingAddress(); 
        str += msgs[i].getMessageBody().toString(); 
      if (specificPhoneNumber.equals(phNum)) 

      { 
       Uri uri = Uri.parse("content://sms/inbox"); 

       ContentResolver contentResolver = context.getContentResolver(); 

       String where = "address="+phNum; 
       Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null, 
            null); 

       while (cursor.moveToNext()) { 

        long thread_id = cursor.getLong(1); 
        where = "thread_id="+thread_id; 
        Uri thread = Uri.parse("content://sms/inbox"); 
        context.getContentResolver().delete(thread, where, null); 

       } 
         Intent l = new Intent(context,AgAppMenu.class);     
         l.putExtra("msg",str);     
         l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         context.startActivity(l); 
        } 
       } 
      } 
    } 
} 
+0

@Snake只是檢查我的答案.. – Rahil2952

1

您可以使用selectionArgs兩個更有效率:

String[] phoneNumber = new String[] { "+18839494492" }; //the wanted phone number 
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "address=?", phoneNumber, null); 

有了這個改變,你只能從想要的號碼中獲得短信,並且你不必抓取所有收到的短信。

相關問題