this is my first time posting here. I'm new to Java & using the Android SDK, and thought I'd start off by trying to write a simple app to filter text messages for "scammy" keywords, for an Innovation module in school.
The app began off by showing a small toast whenever a keyword ("FREEBIE" etc.) is flagged, but I've been trying to make a more noticeable indicator, such as through alert dialogs.
Not sure what I've been doing wrong, but the alert isn't displaying when a message containing the keyword is sent through, but toasts work fine though. I thought it might be an issue with the context, so I've tried context/this/getActivityContext etc., but I receive a "Builder cannot be applied to com.example.myapp.ReceiveSms" error.
package com.example.myapp;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class ReceiveSms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
if (bundle != null) {
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String msgFrom = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
if (msgBody.matches(".*\\bFREEBIE\\b.*")) {
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
builder.setTitle("Scam Message");
builder.setMessage("Scam Message");
builder.setCancelable(true);
builder.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}