如何读取传入的短信

时间:2012-08-31 06:49:39

标签: android

  

可能重复:
  Detecting SMS incoming and outgoing

我想读取传入的短信来检查短信中存在的特定值

我发现这个短信回复代码 我想检查recise sms是否具有以下值xml值

请帮助我查看

//this is response which i check in reciving sms

00
LOG
00
Adeel Aslam
10.00
10/05/2012 10:00
+100


//this is code which i found for sms recive
     public class SmsReceiver extends BroadcastReceiver
  {
@Override
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]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }   

} 

1 个答案:

答案 0 :(得分:0)

您可以使用contains()方法检查短信中是否存在特定字词。

 if(str.contains("LOG"))
  {
    // Perform your task here.
   }