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);
}
}