Search code examples
androidandroid-intentandroid-broadcast

When receiving the sms, only the message was displayed but app didnt open - Android


i am new to BroadcastReceiver concepts.

when the app receives the message it shows the message using toast.makeText. but the app didnt open. how can i open the app please help.

This is my Activiy Class

     public class BroadcastNewSms extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidexample_broadcast_newsms);}}

This is my IncomingSms seperate class when receiving it shows only the message but the app didnt open. how can i do that.

      public class IncomingSms extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                int duration = Toast.LENGTH_LONG;
                    SmsManager sms = SmsManager.getDefault();
                     sms.sendTextMessage("7373457769", null, message, null, null);
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();
            } // end for loop
          } // bundle is null

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

Solution

  • try this:

    public class IncomingSms extends BroadcastReceiver {
        public static final String NUMBER = "NUMBER";
        public static final String MESSAGE = "MESSAGE";
    
        public void onReceive(Context context, Intent intent) {
            final Bundle bundle = intent.getExtras();
            try {
                if (bundle != null) {
                    final Object[] pdusObj = (Object[]) bundle.get("pdus");
                    for (int i = 0; i < pdusObj.length; i++) {
                        int duration = Toast.LENGTH_LONG;
                        SmsManager sms = SmsManager.getDefault();
                        sms.sendTextMessage("7373457769", null, message, null, null);
                        Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                        toast.show();
    
                        // !!!
                        Intent intent = new Intent(context, BroadcastNewSms.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(NUMBER, senderNum);
                        intent.putExtra(MESSAGE, message);
                        context.startActivity(intent);
    
                    } // end for loop
                } // bundle is null
    
            } catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" +e);
            }
        }
    }
    
    
    public class BroadcastNewSms extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.androidexample_broadcast_newsms);
    
            // !!!
            Intent intent = getIntent();
            String number = intent.getStringExtra(IncomingSms.NUMBER);
            String message = intent.getStringExtra(IncomingSms.MESSAGE);
        }
    }