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?
– Jorge B.
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
– Theo Beckham