user2779311
user2779311

Reputation: 1884

Static Broadcast Receiver not being called for SMS_RECEIVED

i am writing a broadcastreciever to listen for SMS_RECEIVED action .it works perfectly fine if i register it dynamically but if i register it statically in the Manifest file the broadcast receiver is not being called.

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

My broadcast reciever is as follows

public void onReceive(Context context, Intent intent) {
    String action=intent.getAction();
        if(action.equals(SMS_RECEIVED_ACTION))
        {
Log.v("sms", "reciever called");
            Object[] messages=(Object[])intent.getExtras().get("pdus");
            for(Object message:messages)
            {
                byte[] messagedata=(byte[])message;
                SmsMessage smsmessage=SmsMessage.createFromPdu(messagedata);
                processmessage(smsmessage,context);
            }

        }


    }

    private void processmessage(SmsMessage smsmessage,Context c) {
        String from=smsmessage.getOriginatingAddress();
        String messcontent=smsmessage.getMessageBody();
        Toast.makeText(c,"from ="+from+" and mess="+messcontent,Toast.LENGTH_LONG).show();
        Log.v("sms","from ="+from+" and mess="+messcontent);
}

My Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tech.myfirst" >

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

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <activity
            android:name=".SmsReceiverDemo"
            android:label="SmsReciever Demo"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.tech.myfirst.MyReceiverBroadcast"><intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
        </intent-filter></receiver>

    </application>


</manifest>

I have added all the necessary permissions. What i am missing here. i am running the program on emulator nexus 5 api 21. thanks

Upvotes: 0

Views: 434

Answers (2)

Mike M.
Mike M.

Reputation: 39191

Assuming everything else is correct, it appears the problem is that you've misspelled RECEIVED in the <intent-filter>'s <action> for the <receiver> entry in the manifest. It should be:

<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>

You might also want to check the value of the SMS_RECEIVED_ACTION String.

Upvotes: 1

Marcus
Marcus

Reputation: 6717

I've had this problem before, and was solved by changing the attribute

android:name=".MyReceiverBroadcast"

To

android:name="my.package.name.MyReceiverBroadcast"

I don't know why this is happening, but it worked for me.

Upvotes: 0

Related Questions