Compare SMS data with txt file

1

I'm trying to create an application in which I get an SMS that will contain a code, example "AC0012", so I'll read this information, and compare it with with my file .txt .

This .txt file will contain several lines, and each line will have information like this: "AC0012 - Avenida São Carlos - Centro".

So if the SMS code is equal to the code of .txt , I'm going to play on the screen "Avenida São Carlos - Centro" and make a sound as if a new message had arrived. I do not know if the file in .txt is the best to do this. I even tried to read the file that is in the Asset folder and then compare the string of the line by line to the file .txt , but could not even read the file right, has made several errors.

So I deleted everything and came after help to implement this app.

Below is the basics I've done.

Class that receives the SMS and transforms it into a String

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


public class RecebeSMS extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Recebe o sms
        Bundle bundle = intent.getExtras();
        Object[] messages=(Object[])bundle.get("pdus");
        SmsMessage[] sms=new SmsMessage[messages.length];
        String codigo = "";

        if (bundle != null){
        // Recuperando mensagem recebida
            for (int i = 0; i<messages.length; i++){
                sms[i]= SmsMessage.createFromPdu((byte[]) messages[i]);
                codigo += ", Codigo: ";
                codigo += sms[i].getMessageBody().toString();
                codigo += "\n";
            }


        }

    }
}

Main class:

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

public class givetheaddress extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_givetheaddress);
    }
}
    
asked by anonymous 23.02.2016 / 10:12

1 answer

2

If the amount is too large, I suggest you save the information in a database.

Otherwise, to read a txt file located in the Assets folder:

 public void searchCode (String smsCode){
        try {
            AssetManager assetManager = getResources().getAssets();
            InputStream inputStream = assetManager.open("nome-do-arquivo.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String linha ="";

            while(linha !=null){
                linha = bufferedReader.readLine(); // lemos linha por linha
                if(linha.contains(smsCode)){
                    /**
                     * Aqui vai suas ações
                     */
                }

            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Here's an example , which explains how to create the folder!

Another option is to create the list in String Resource Android:

Within the res > values folder there is a file called strings.xml

Follow the documentation

You can also create a file with a specific name, for example:

Codigos.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name=“codigos_array“>
   <item>AC0012 - Avenida São Carlos - Centro</item>
   <item>AC0013 - Avenida B - Bertioga</item>
   <item>AC0014 - Avenida C - São João</item>
  </string-array>
</resources>

To read the array:

public void searchCode (String smsCode){
    final String[] values = getResources().getStringArray(R.array.codigos_array);

        for(String linha : values){
            if(linha.contains(smsCode)){
                /**
                 * Aqui vai suas ações
                 */
            }

        }

}
    
23.02.2016 / 12:11