2

I am new to android programming please help me in resolving a problem.

My code to receive sms is not working.

the manifest file is

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

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

while the java code

package com.android.SMS;


import android.os.Bundle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import  android.telephony.*;
import android.telephony.gsm.SmsMessage;
import android.util.Log;

import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{

     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();
            }                         
        }


}

Please help me in resolving the problem. I am stuck into this from yesterday and i am failed to see any problem in the code.

3
  • Can you be a little more specific about what "not working" means? Does it throw an exception? Are there any errors / warnings in your logcat output? Jun 14, 2011 at 6:24
  • are you testing it in the emulator?
    – Azlam
    Jun 14, 2011 at 9:09
  • I am doing something similar HERE!!! stackoverflow.com/questions/14452808/… Jan 22, 2013 at 8:17

5 Answers 5

1

Here is what I have working for me at the moment. The code that I'm providing is used to block incoming text messages, but you can easily modify it to only include the area where it only alerts you of incoming messages and doesn't process them any further.


SmsReceiver.java

package com.android.SMS;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

public static int MSG_TPE=0;
private String getAddress;
public void onReceive(Context context, Intent intent) { 
    String MSG_TYPE=intent.getAction();
        if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
            Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
            received.show();

                Bundle bundle = intent.getExtras();
                Object messages[] = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];
                for (int n = 0; n < messages.length; n++) {
                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                }

                    getAddress = smsMessage[0].getOriginatingAddress();
                    // Filter incoming messages
                    if(getAddress.equals("APPROVEDPHONENUMBER")) {
                        Toast approved = Toast.makeText(context,"Approved SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
                        approved.show();
                            // Message is approved and let through
                    } else {
                        Toast blocked = Toast.makeText(context,"Blocked SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG);
                        blocked.show();
                            // Message is blocked
                            abortBroadcast();
                    }
                    // End filter
                        for(int i=0;i<8;i++) {
                            System.out.println("Blocking SMS");
                        }

        }

}

}


This is the code that detects an incoming message

if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
        Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
        received.show();
}



AndroidManifest.xml

PERMISSIONS:

<uses-feature android:name="android.hardware.telephony" /> 
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />


APPLICATION BLOCK:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >


    <service android:name=".MyService" android:enabled="true"/>
     <receiver android:name="SmsReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_SENT"/>
            </intent-filter>
     </receiver>
    <service android:name=".MyServiceSentReceived" android:enabled="true"/>
         <receiver android:name="SmsReceiver">
                <intent-filter android:priority="2147483645">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                </intent-filter>
         </receiver>

</application>

It's important to place the service and receiver blocks inside of your main "application" block as shown in the code above.

1
package com.google.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {    
    @Override /** This line is important, as you have not overriden the original method*/
    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();
        }
    }
}

I hope this will help you.

1

You may need to add the BROADCAST_SMS permission to your receiver

<receiver android:name=".SmsReceiver" android:enabled="true"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
1

Ensure your App has permission on the device/emulator:

On the device/emulator go to... Settings -> Privacy -> Permission manager -> SMS

Ensure your App is in the ALLOWED list. If it's in the denied list, click on it and select 'Allow'

I went round in circles for an hour or two wondering why my code wasn't being called until I realised this.

0

delete this line. import android.telephony.gsm.SmsMessage;

please be sure that SmsMessage is must class under "android.telephony".

This may help you... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.