SMS_RECEIVED onReceive android 2.3.5 not triggering
Asked Answered
G

4

1

I've been puzzling over this one recently, prior to 2.3.5 this seems to work fine (on my colleagues devices). However on mine it now never triggers.

I've stripped the code right back and made a very simple test application to see what's going on. Basically the onReceive code doesn't ever appear to trigger, even though adb/logcat does seem to show the register of the BroadcastReveiver does take place.

Here's the simple code I've gone for:

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

<uses-sdk android:minSdkVersion="9" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".TestSMSReceiveActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".mysmstestcall">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

Then:

package com.broadcasttech.testsmsreceive;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class TestSMSReceiveActivity extends Activity {
private BroadcastReceiver receiver;
private static final String TAG = "TestSMSApp";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, " App has started up");
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    Log.i(TAG, " Filter SMS_RECEIVED has been added");
    //Extends BroadcastReceiver
    receiver = new mysmstestcall();
    registerReceiver(receiver,filter);
    Log.i(TAG, " registerReceiver sorted");
}

//Also, to save headaches later
@Override
protected void onDestroy() {
  Log.i(TAG, " unregistering Receiver");
  unregisterReceiver(receiver);
  Log.i(TAG, " done");
}
}

And finally

package com.broadcasttech.testsmsreceive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class mysmstestcall extends BroadcastReceiver {

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

@Override
public void onReceive(Context context, Intent intent) {
     Log.i(TAG, "Intent recieved: " + intent.getAction());
     if (intent.getAction() == SMS_RECEIVED) {
         //any action you want here..
         Log.i(TAG, "SMS received has triggered");
     }
}
}

So it is a fairly simple app that should just log and tell me when the BroadcastReceiver is triggered, but it just won't fire at all.

Can anyone suggest whats wrong, I've checked various tutorials, checked as I know IceCreamSandwich is different, but tried to incorporate those fixes too and this doesn't make a difference either.

Thanks in advance!

Grayson answered 10/1, 2012 at 12:12 Comment(9)
your manifest is missing the uses-permission required to catch the SMS received broadcast. Other than that, have you installed any SMS-applications from Android Market (or other places) - some of these will block other applications (just for shits and giggles) from receiving the ordered broadcast by registering at a very high priority and cancelling the broadcast intent.Richelle
Oops, that was the editor removing the uses-permission. I've corrected that, thanks for the heads up (My app does have it so that wasn't the key). I do however have 'Handcent SMS' installed so I wonder if this could be doing the ordered broadcast. Is there any way around this?Grayson
Uninstall Handcent and test again. The registration is first-come-first-serve (in the case where two receivers are registered for the same priority - if i remember correctly) if the developers of Handcent SMS is doing stupid and irresponsible things. You can always show a warning in your application that the user has malicious software installed that may interfere with the operation of this application (i.e. Handcent) if this is the case.Richelle
it is unlikely that an application can change the the order of a system level broadcast. It cannot mimic it, therefore it cannot change it. System level broadcasts are protected and cannot be replicated. And if an application could stop another from receiving a broadcast, that would be a HUGE problem in the frameworks which most likely is NOT the case.Raleighraley
You've hit it spot-on Jens. I didn't uninstall Handcent (Now I think about it, I have some other remote control software via SMS too which could do the same). So I just changed my app to use: <intent-filter android:priority="1000"> And sure enough, it worked perfectly.Grayson
Rather worryingly JoxTraex this is EXACTLY what is happening. Some other app (May or may not be Handcent) was stopping any other broadcast receivers from firing. The only change I made to my app was the priorty of the intent-filter, and success!Grayson
@JoxTraex: The SMS received broadcast is an ordered broadcast, i.e. it can be cancelled. Such is the design of the system, and apparently, allowing arbitrary applications to block each other out is par for the course (I've seen more of these questions here on StackOverflow where the culprit was some rogue SMS application dl:ed from Market). Some of these jokers max out the priority, effectively forcing you to uninstall their crap to get other SMS applications to work. Fortunately it's quite possible for other applications to detect these morons.Richelle
Good to know, however; it still makes no sense that that should happen. Hopefully its handled in ICS.Raleighraley
Afaik, the launch-control fixes in Honeycomb (i.e. a package that is stopped (or never started) will not receive broadcast notifications unless the sender explicitly configures it) will affect SMS applications in ICS also - but they can still block the sh*t out of each other as long as you don't force-stop them as far as I can see.Richelle
T
2

The main problem is that this line in "mysmstestcall" is wrong:

if (intent.getAction() == SMS_RECEIVED)

should be changed to this:

if (intent.getAction().equals(SMS_RECEIVED)) 
Tragopan answered 17/5, 2012 at 16:59 Comment(0)
R
0

Is there a reason that you have two recievers? You have a programatic listener and you have an XML listener

Programatic:

filter.addAction("android.provider.Telephony.SMS_RECEIVED");
Log.i(TAG, " Filter SMS_RECEIVED has been added");
//Extends BroadcastReceiver
receiver = new mysmstestcall();

XML Listener:

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

Are you sure you want these two? If you have two you will RECEIVE the same broadcast 2 times...

As for another ISSUE that I see is this line

if (intent.getAction() == SMS_RECEIVED)

When comparing strings you DO NOT compare them like this instead you'd have something like this:

if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED))
Raleighraley answered 10/1, 2012 at 13:39 Comment(1)
This is because in newer OS released (IceCreamSandwich), etc, it is recommended to do it this way. AFAIK it will not register the same entity twice, so won't cause two alerts.Grayson
S
0

I've got this once. It can be network's issue. These were my question and answer. You can add a sending intent and catch the result code. In my case, it was RESULT_ERROR_GENERIC_FAILURE. I tried to find other solutions for months but no luck, so I accepted that even though I don't want to :-(

Sinegold answered 22/7, 2012 at 1:22 Comment(0)
T
0
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 SmsBroadcastReceiver extends BroadcastReceiver{
    public static final String SMS_BUNDLE = "pdus";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, "A new message is added to the SMS List!!!\n"+smsMessageStr, Toast.LENGTH_SHORT).show();

            //this will update the UI with message
            InboxMain inst = InboxMain.instance();
            inst.updateList(smsMessageStr);

    }

}
}


manifest::
<receiver android:name=".SmsBroadcastReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
    <intent-filter android:priority="2147483647" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
 </receiver>

permission::
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
Tough answered 16/4, 2015 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.