如何逐行读取短信响应行 [英] How to read sms response line by line

查看:138
本文介绍了如何逐行读取短信响应行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code阅读短信。我想读通过线接收短信行,如果行一个不包含00这样做,如果2号线包含此字符串这样做等。

下面是我的code,只能读取包含00那么这样做不是逐行读取我如何改变这个code到逐行读取短信行短信

 公共无效的onReceive(上下文的背景下,意图意图)
{
    // ---得到通过短信---
    捆绑包= intent.getExtras();
    SmsMessage []封邮件= NULL;
    字符串str =;

    如果(捆绑!= NULL)
    {
        // ---检索收到的短信---
        [对象]的PDU =(对象[])bundle.get(的PDU);
        封邮件=新SmsMessage [pdus.length]
        的for(int i = 0; I< msgs.length;我++){
            封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
            STR + =短信来自+封邮件[I] .getOriginatingAddress();


            STR + =;
            STR + =封邮件[I] .getMessageBody()的toString()。拆分(/ N);;
            STR + =\ N的;






            如果(str.contains(00));
            {


                 意图L =新的意图(背景下,AgAppMenu.class);
                 l.putExtra(味精,STR);
                 l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 context.startActivity(升);


            }
 

解决方案

使用的 String.split()

例如,从你做一个字符串获得不同的行:

 字符串例如=你好\ nWorld!;
的String []部分= example.split(\ N);

//部分[0]包含你好
//部分[1]包含世界!
 

有关你的情况,得到消息的每一行,使用这样的:

 的String [] msg_lines =封邮件[I] .getMessageBody()的toString()分裂(/ N)。
 

所以,你的code可能看起来像:

 的String [] msg_lines = NULL;
对于(...) {
    封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
    STR + =短信来自+封邮件[I] .getOriginatingAddress();
    STR + =;
    STR + =封邮件[I] .getMessageBody()的toString()。
    STR + =\ N的;

    msg_lines =封邮件[I] .getMessageBody()的toString()分裂(\ N)。
    对(串线:msg_lines)
    {
        如果(line.contains(00)){/ *然后* /}
    }
}
 

This is my code to read sms. I want to read received sms line by line if line one contain no 00 do this if line 2 contain this String do this etc.

Below is my code which only read smss containing 00 then do this not read line by line how I change this code to read sms line by line

  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().split("/n");;
            str += "\n"; 






            if(str.contains("00"));
            {


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


            }

解决方案

Use String.split().

For example to get the different lines from a String you do:

String example = "Hello\nWorld!";
String[] parts = example.split("\n");

// parts[0] contains "Hello"
// parts[1] contains "World!"

For your case, to get each line of the message, use something like :

String[] msg_lines = msgs[i].getMessageBody().toString().split("/n");

So your code may look like:

String[] msg_lines = null;
for(...) {
    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
    str += "SMS from " + msgs[i].getOriginatingAddress();                     
    str += " :";
    str += msgs[i].getMessageBody().toString();
    str += "\n";

    msg_lines = msgs[i].getMessageBody().toString().split("\n");
    for (String line : msg_lines)
    {
        if (line.contains("00")) { /*then*/ }
    }
}

这篇关于如何逐行读取短信响应行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆