12

I'm trying to write a simple application that attempts to receive SMS messages and handle them. I've followed several tutorials but I'm getting nowhere, when I send a SMS to the emulator, the Intent never seems to get fired.

Here is my intent:

package com.neocodenetworks.smsfwd;

import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.Log;

public class SmsReciever extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "smsfwd";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == 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) {
                    Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                    NetComm.SendMessage("me", messages[0].getOriginatingAddress(), messages[0].getMessageBody());
                }
            }
        }
    }
}

and here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.neocodenetworks.smsfwd"
        android:versionCode="1"
        android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <receiver android:name=".SmsReciever">
            <intent-filter>
                <action android:name="android.provider.telephony.SMS_RECIEVED"></action>
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="6" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

I'd really appreciate some guidance with what's going wrong. I'm just getting into Android development but I think I have my head wrapped around (most of) it. While monitoring the emulator's logcat, the log events never come up, and debugging breakpoints are never hit, so I have a feeling it's somewhere in my intent filter.

I'm running this on Android 2.0.1.

Jess
  • 42,368
  • 6
  • 37
  • 51
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – Etienne Lawlor Jan 22 '13 at 08:12
  • For more suggestions into why an SMS broadcast received isn't being fired, see this post: http://stackoverflow.com/questions/4117701/android-sms-broadcast-receiver?rq=1 – TheIT Jan 15 '14 at 00:30

4 Answers4

10

I think your manifest looks okay; the problem is with the line:

if (intent.getAction() == SMS_RECEIVED) {

I think it should be: intent.getAction().equals(ACTION)

Hope that helps..

Samuh
  • 36,316
  • 26
  • 109
  • 116
10

In addition to Samuh's answer (you need to do an object comparison on the action string, or just do no comparison), in your manifest file you have misspelled SMS_RECEIVED.

Community
  • 1
  • 1
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 1
    In case you are using Eclipse with ADT, you can avoid such errors by adding permissions using the Permission Tab that the Manifest Editor provides rather than typing them yourself. – Samuh Dec 23 '09 at 04:01
  • While Samuh is correct, the fact I had misspelled it was what was stopping it from working. Thanks! – Jess Dec 23 '09 at 05:08
1

The action name seems to require a capital "T" in "Telephony.

android.provider.Telephony.SMS_RECEIVED

1

For String variables you should not compare with == symbol, you should compare with equals() method

this is wrong

intent.getAction() == SMS_RECEIVED) 

this is right

intent.getAction().equals (SMS_RECEIVED)) 
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
naveen
  • 11
  • 1