Compare SMS data with txt file

Asked

Viewed 77 times

1

I am trying to create an application where I receive an SMS that will contain a code, example "AC0012", so I will read this information, and compare it with my file .txt.

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

So if the SMS code matches the code of the .txt, I will play on the screen "Avenida São Carlos - Centro" and emit a sound as if it had arrived new message. 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 folder Asset and then compare the string from code line to file line .txt, but I could not even read the file right, gave several errors.

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

Below the basics I’ve done.

Class that receives the SMS and turns 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);
    }
}
  • How about using a Database? You are programming on Android?

  • It would be a good, I even looked for something on Sqliteopenhelper, but as I’m new messing with Android I thought it would be more complex. But come on, I accept suggestions :D

1 answer

2

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

Otherwise, to read a file txt 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 is an example, which even explains how to create the folder!

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

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

Follows the documentation

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

xml codes.:

<?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
                 */
            }

        }

}

Browser other questions tagged

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