1

I am having issues getting my broadcast receiver to fire. My code is as follows:

<receiver android:name="com.test.android.receivers.SMS" >
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>




public class SMS extends BroadcastReceiver {
private final String TAG = this.getClass().getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle pudsBundle = intent.getExtras();
    Object[] pdus = (Object[]) pudsBundle.get("pdus");
    SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
    Logger.sendLog(TAG, messages.getOriginatingAddress());
}

}

The device is a HTC One X running a fresh version of CM.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.test.android.UIMain"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.test.android.UISettings"
        android:label="@string/title_activity_uisettings" >
    </activity>

    <service android:name=".services.LocService" >
    </service>

    <receiver android:name="com.test.android.receivers.SMS" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

</manifest>
JamieB
  • 923
  • 9
  • 30

3 Answers3

0

You must use READ_SMS permission into your Manifest before having at least 1 chance to intercept your SMSes.

 <uses-permission android:name="android.permission.READ_SMS" /> 
Cob013
  • 1,057
  • 9
  • 22
  • and RECEIVE_SMS have been added with no difference. – JamieB Jun 23 '13 at 17:26
  • Have you tried to read old reference on SO about BroadcastReceiver class? I've found many question about people struggling with this class. Try this for example http://stackoverflow.com/questions/4117701/android-sms-broadcast-receiver – Cob013 Jun 23 '13 at 17:29
0

add this line to your android manifest file

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

This line goes directly under the <manifest> tag, as follows:

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

    </application>
</manifest>

Update

I found This in the source code of the default android mms application:

<!-- Require sender permissions to prevent SMS spoofing -->
<receiver android:name=".transaction.PrivilegedSmsReceiver"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

The comment on the block gives me the impression that you need to acquire the <uses-permission android:name="android.permission.SEND_SMS" /> too.

Try it and let me know if that worked.

kdehairy
  • 2,630
  • 22
  • 27
  • As mentioned in another comment, this is already the case with my application. – JamieB Jun 23 '13 at 17:31
  • in the `` do you have the attribute `android:enabled` set to false?. if so, your receiver will be disabled too. which means, the system will not be able to call your receiver. – kdehairy Jun 23 '13 at 17:50
  • I've added my manifest for viewing. I do not have enabled set to false. – JamieB Jun 23 '13 at 17:54
  • I updated my answer. I don't know if this will help. but, you won't loose any if you tried :) .. let me know if it worked! – kdehairy Jun 23 '13 at 19:43
0

set android:exported="true" inside your receiver tag. This will make your receiver available to other application. Check this out for more info.

So your final code should look like,

<receiver android:name="com.test.android.receivers.SMS" 
          android:exported="true" >
        <intent-filter

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

I think this should solve your issue.

Dulanga
  • 1,326
  • 7
  • 15
  • Unfortunately this didn't solve the issue. Thanks for the input. – JamieB Jun 23 '13 at 18:26
  • Sorry I have written a wrong code. See the edited one. Tell me if this doesn't work. – Dulanga Jun 23 '13 at 19:06
  • 1
    `android:exported` is set to `true` by default if you provided an `` in your declaration. so, it actually is `true` here without saying it explicitly. – kdehairy Jun 23 '13 at 19:45
  • Thanks a lot for that.Sorry for this answer I thought it is always false regardless of . I am wrong – Dulanga Jun 23 '13 at 20:23