Read a received SMS

Asked

Viewed 3,179 times

6

An App receives an SMS and from the text performs a function. For this it is necessary to read the SMS and make the comparison in the APP, but for this must read, word by word, line by line or make a comparison if it contains such a word in this SMS.

SMS will have the following content.

If a change of status occurs:
MONIT-110
E05-ATI
E06-DES

And that of all exits: STATUS-110
E01-DES
E02-DES
E03-DES
E04-DES
E05-DES
E06-DES
E07-DES
E08-DES
E09-DES
E10-DES
E11-DES
S01-DES
S02-DES
S03-DES
S04-DES
S05-DES
S06-DES

For each of these lines have a different command to execute.

Code responsible for MSG received:

public class ReceberSms extends BroadcastReceiver {
    private static final String CATEGORIA = "acqua";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(CATEGORIA, ">" + intent.getAction());

        Sms sms = new Sms();
        //Lê a mensagem
        SmsMessage msg = sms.receberMensagem(intent);
        String celular = msg.getDisplayOriginatingAddress();
        String mensagem = msg.getDisplayMessageBody();
        //pegar essa msg e jogar nas configurações

        String texto = "ReceberSms: recebeu sms[" + celular + "] -> " + mensagem;
        Log.i(CATEGORIA, texto);
        Toast.makeText(context, texto, Toast.LENGTH_SHORT).show();
}

}//fim
  • I didn’t quite understand what the doubt is... But I believe it’s like breaking the text in tokens?

  • The text that I receive contains commands to doubt is how I can read the content of the text to identify the commands that are in it. I think it would be like breaking the text into tokens but I don’t know how to do it.

  • 2

    Seeing that you already have the full text, I think just call the method split, passing the command tab. It would be nice to maybe include an example to help find the solution.

2 answers

1

It may be necessary to build it through an object vector. Note this code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveSMSActivity extends Activity{

    static TextView messageBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        messageBox=(TextView)findViewById(R.id.messageBox);
    }

    public static void updateMessageBox(String msg)
    {
        messageBox.append(msg);
    }

}

This one detects and processes the message:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class TextMessageReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent)
    {
        Bundle bundle=intent.getExtras();

        Object[] messages=(Object[])bundle.get("pdus");
        SmsMessage[] sms=new SmsMessage[messages.length];

        for(int n=0;n<messages.length;n++){
            sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
        }

        for(SmsMessage msg:sms){
            ReceiveSMSActivity.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
                    "Message: "+msg.getMessageBody()+"\n");
        }
    }
}

I think it lacks as another user said, a command, see if you can with this model above.

0

If the string la of the end of your code already contains line, message, which you read in the line below:

String mensagem = msg.getDisplayMessageBody();

You can use Split, as already mentioned by @Wakim, by breaking lines and dopois by '-', and finally a Switch to execute the commands, would look something like this:

String[] linhas = mensagem.split("\n"); // Veja qual o seu caracter de fim de linha.

for (String l : linhas){
   switch (l) {
         case "E01-DES":
             ...
             break;
         case "E02-DES":
             ...
         case ...

             break;
         default:
             Log.i("ALERTA","Sinal recebido não suportado: " + l);
     }
}

Note that this code is just one example, I would do this function with the Switch outside of FOR, or I would separate each line into two new Strings, and maybe do a generic function to perform each of the actions. But I’d need more information of your code for that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.