1

I have written a SMS receiver for catching income SMS , everything looks fine but it doesn't works and no SMS income received by the receiver . this is the codes and manifest content. As I remember I had same app has written in android 2.3 working fine but this code is running in android 4.x which is not functioning properly. what is the problem ? Is it depends on security issues of android 4.x ?

Manifest:

<receiver android:name="SmsReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

Java Code:

 public class SmsReceiver extends BroadcastReceiver {

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Logger.i("INCOMMING SMS...");
    if (action == SMS_RECEIVED) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            if (messages.length > -1) {
                String sendr = messages[0].getOriginatingAddress();
                Logger.i(sendr);
            }
        }
    }
}


}
Bahram
  • 1,464
  • 2
  • 22
  • 38

2 Answers2

2

I had the same problem and I've fixed it by adding two more actions to the manifest registered receiver so it will look like this:

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="100">
    <action android:name="android.intent.action.PHONE_STATE"/>
        <action android:name="android.provider.telephony.SMS_RECEIVED"/>
        <action android:name="com.your.package.android.action.broadcast"/>
    </intent-filter>
  </receiver>

also add permission :

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

And for comparsion of Strings do not use equals operator, but equals method instead.(Note. equalsIgnoreCase() should be better for you.)

so it will be like:

if(SMS_RECEIVED.equalsIgnoreCase(action))
{
//continue
}

Hope it helps.

Helmisek
  • 1,209
  • 11
  • 12
0

I had the same problem as you and after much research on the net, I found the problem. Your application is not started and the Emulator Control doesn't send the option --include-stopped-packages as the adb command can do it. It is a feature introduced with Android 3.x.

So, your receiver never receives the broadcast because your application is not started. To start it first, open a console from your system and type the following command:

*adb -e shell am broadcast -a android.provider.Telephony.SMS_RECEIVED --include-stopped-packages*

Your BroadcastReceveiver receive an empty SMS.

After this command, your process is displayed in the Devices view, and you can send SMS from your emulator control.

CuberChase
  • 4,458
  • 5
  • 33
  • 52