0

I am new to Android programming and currently learning it for an app. I am trying to intercept the SMSs that are received and extract information from them. This may be a duplicate question but I need help specific to my problem. My broadcast receiver is not getting called.I have declared the receiver in the Manifestfile and created a receiver class that extends to BroadcastReceiver Where am I going wrong.

ManifestFile:

<receiver android:name="com.example.receivingsms.SMSReceiver"> 
<intent-filter> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>

SMSReceiver.java:

public void onReceive(Context context, Intent intent) {
     //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        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]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }           
}
android_eng
  • 1,370
  • 3
  • 17
  • 40

0 Answers0