2012-06-06 31 views
0

我構建應用程序,將讀取收件箱 每一個新的短信,這是從特定發件人,我的應用程序將讀取的內容,如果它有一些具體的內容,那麼它會做一些動作。閱讀郵件內容和郵件發件人?

目標:

我想獲得新的郵件發件人的姓名或號碼(說我特定發件人不顯示號碼是像TM-Google , TM-MyGinger大多telemarketting發件人

2.如果是來自我正在尋找的人,那麼我想讀取消息的內容。 其他是我的一部分。 請提供一些想法或代碼片段。

+0

你試過聆聽每一個短信,你需要使用ContentObserver觀看收件箱 –

回答

2

創建短信接收器

public class SMSReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 

     Bundle bundle = intent.getExtras(); 

     if (bundle != null) { 
       Object[] pdusObj = (Object[]) bundle.get("pdus"); 
       SmsMessage[] messages = new SmsMessage[pdusObj.length]; 

       // getting SMS information from Pdu. 
       for (int i = 0; i < pdusObj.length; i++) { 
         messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
       } 

       for (SmsMessage currentMessage : messages) { 
        // currentMessage.getDisplayOriginatingAddress() has sender's phone number 
        // currentMessage.getDisplayMessageBody()  has the actual message 
      } 
     } 
    } 
} 

您可以使用下面的代碼讀取收件箱:

Uri mSmsinboxQueryUri = Uri.parse("content://sms"); 
      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()); 
       Log.e("Count",count); 
       while (cursor1.moveToNext()) { 
        String address = cursor1.getString(cursor1 
          .getColumnIndex(columns[0])); 
        String name = cursor1.getString(cursor1 
          .getColumnIndex(columns[1])); 
        String date = cursor1.getString(cursor1 
          .getColumnIndex(columns[2])); 
        String msg = cursor1.getString(cursor1 
          .getColumnIndex(columns[3])); 
        String type = cursor1.getString(cursor1 
          .getColumnIndex(columns[4])); 
        et.setText(et.getText() + "Address:" + address + "\n" 
          + "Name:" + name + "\n" 
          + "Date:" + date + "\n" 
          + "MSG:" + msg + "\n" 
          + "type:" + type + "\n" 
          ); 



       } 
      } 

添加流向表現

<receiver android:name="SMSReceiver" android:enabled="true"> 
    <intent-filter> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
    </receiver> 

READ_SMSRECEIVE_SMS

添加權限
+0

天氣它將能夠讀取非數字形式的發件人地址 – Trikaldarshi

+0

是的。如果發件人是'TM-MyGinger',則地址返回'TM-MyGinger' – Sandy

+0

好的,謝謝............. – Trikaldarshi