0

I am developing an app which will react when a text message is received. The code I have got crashes my app when a text message is received but due to having to install the app on my own device I am unable to get any crash reports to identify the problem at the time of the crash.

The code shown no errors before its built using android studios.

    public class ReceiveTextMessage extends BroadcastReceiver{

    private SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                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]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

Can anyone give me a clue as to why the crash may occur or anyway I could find a crash report.

I have edited my code in many different ways all giving the same result.

  • have registered the receiver in manifest? Have you given permissions? have you requested runtime permissions? Also, be sure intent not null..... – Opiatefuchs Jun 14 '16 at 11:34

1 Answers1

1

Posting my own running code :

    public class SmsReceiver extends BroadcastReceiver {
        static final String ACTION =
                "android.provider.Telephony.SMS_RECEIVED";

        @SuppressWarnings("deprecation")
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION)) {
                Bundle pudsBundle = intent.getExtras();
                Object[] pdus = (Object[]) pudsBundle.get("pdus");
                SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
                String phoneNumber = messages.getOriginatingAddress();
                if (phoneNumber != null && phoneNumber.contains(Constants.OTP_NUMBER)) {
                    this.abortBroadcast();
                    Log.i("ABORTED", "DONE");
                    Intent localIntent = new Intent(Constants.OTP_BROADCASTING_PACKAGE);
                    String otp = messages.getMessageBody();
                    if (otp != null && !otp.equals("")) {
                        otp = otp.substring(otp.length() - 5, otp.length() - 1);
                        localIntent.putExtra("message", otp);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent);
                    }
                }
            }
        }
    }

**In Manifest**

    <receiver android:name="your_path.SmsReceiver">
                <intent-filter android:priority="1000">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>

Permission :

<uses-permission android:name="android.permission.RECEIVE_SMS" />

Hope it will help you :)

Neo
  • 3,546
  • 1
  • 24
  • 31
  • 1
    After editing some code to implement my own functionality this worked!! Much appreciated. –  Jun 14 '16 at 12:02
  • Be carefull though with using **deprecated** methods - [read this](http://stackoverflow.com/a/2941912/4782930) – Strider Jun 14 '16 at 14:18