I have a service in my application that reads incoming messages and performs an action based on the content of the message. If the message contains the keyword, the service opens another activity and if the message does not contain the keyword the service displays an error message in a Toast.
Now I would like that when the message contains the keyword, the service opens the next activity but never runs again. How can I do this? Cordially...
That is my service:
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
assert pdusObj != null;
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
mobile = phoneNumber.replaceAll("\\s", "");
body = message.replaceAll("\\s", "+");
Log.i("SmsReceiver", "senderNum: " + phoneNumber + "; message: " + body);
}
// Show Alert
if (body.contains(keyWord_code)){
Intent i = new Intent();
i.setClass(context, Test.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else{
Toasty.error(context, "Une erreur s'est produite. Veillez vous assurer que vous avez envoyer le montant requis ou contactez nous pour toute requête!", Toast.LENGTH_LONG, true).show();
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}