0

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);
    }
}
  • use `sendBroadcast()` method – Manoj Perumarath Aug 21 '19 at 10:02
  • I am a simple learner please tell me please how to use this please! –  Aug 21 '19 at 10:06
  • `BroadcastReceiver` are used whenever an event is happened. Are you sure that you need a broadcast in your case. If your event is a custom one, you need to create a custom broadcast – Manoj Perumarath Aug 21 '19 at 10:08
  • can you check using log or debugger, whether the control is going to any of the if or else condtion when a sms is received? – Hari N Jha Aug 21 '19 at 10:11
  • Forgive me but I do not understand much. I just wanted to prevent my service from running again if the condition is true if there is a simple way to do this please tell me please! –  Aug 21 '19 at 10:15
  • If you really only want that Receiver to be active until you receive the desired message, you can disable it after you've received that message: https://stackoverflow.com/a/6529365. Though that would be the most efficient solution, you might want to offer the user an option to re-enable it, depending on your needs. Alternatively, you can set a flag in `SharedPreferences` that'll tell you if you've run some code once already, but that will reset if the user clears your app's data from the device Settings: https://stackoverflow.com/a/7065444. – Mike M. Aug 21 '19 at 10:18

1 Answers1

0

note :you can use

Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

in your broadcast class but this may kill your activity in onDestroy() method

                        Intent i1 = new Intent(context, ReadTheMessage.class);
                        i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);//note
                        i1.putExtra("num", smsFrom);
                        i1.putExtra("msg", smsBody);
                        context.startActivity(i1);