0

I have an android application that receives and reads incoming SMS and I would like to start another activity when the message contains a specific word. I tried this but it does not work.

That is my Broadcasreceiver:

final SmsManager sms = SmsManager.getDefault();
String mobile, body;
String keyWord_code = "ovh";

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)){
      context.startActivity(new Intent(context, Test.class));
      //Toast toast = Toast.makeText(context, "senderNum: " + mobile + ", message: " + body, Toast.LENGTH_LONG);
      //toast.show();
    }else {
      Toast toast = Toast.makeText(context, "senderNum: " + mobile, Toast.LENGTH_LONG);
      toast.show();
    }

        } // end for loop
        // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}

and here is the activity that I want to open:

public class Test extends AppCompatActivity {
private static final String TAG = "Test";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}

The code above works at half. that is, when I receive a message, the application parses the message and displays the contents and number of the sender in a Toast but does not open Test.class!

that is my manifest:

 <uses-permission android:name="android.permission.BROADCAST_SMS"
tools:ignore="ProtectedPermissions" />
 <uses-permission android:name="android.permission.RECEIVE_SMS" />
 <uses-permission android:name="android.permission.READ_SMS" />
 <uses-permission android:name="android.permission.SEND_SMS" />

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundIcon="@mipmap/ic_launcher_round"
 android:supportsRtl="true"
 android:theme="@style/AppTheme"
 tools:replace="android:icon">
 <activity android:name=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
 </activity>

 <receiver
  android:name=".SMSListener"
  android:enabled="true"
  android:exported="true">
  <intent-filter android:priority="1000">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter>
</receiver>

<activity android:name=".Test"/>
</application>

I tried on Android KITKAT and OREO but it still does not work. I tried to open an AlertDialog end to allow the user to click himself to open Test.class but the AlertDialog is not displayed. I need help please!

  • you can try deep link. might be a better way to think about it or start. – letsCode Aug 19 '19 at 17:57
  • how am I supposed to do that? –  Aug 19 '19 at 18:03
  • Did you check your logs for that `Log.e()` in the `catch`? You're kind of swallowing the Exception, there, btw. You should either `e.printStackTrace();`, or change the log call to `Log.e("SmsReceiver", "Exception smsReceiver", e);`; comma instead of plus. Anyhoo, check that, and also have a look here: https://stackoverflow.com/q/3689581. – Mike M. Aug 19 '19 at 18:22

0 Answers0