1

I am not able to catch the intent when a SMS is received. Below is my code of the service. I am able to catch the "Intent.ACTION_SCREEN_ON" and "Intent.ACTION_SCREEN_OFF" but I am NOT able to catch the SMS_RECEIVED intent "android.provider.Telephony.SMS_RECEIVED".

Sincerely appreciate any hints on what I am doing wrong here?

public class SmsCatcher extends Service{

BroadcastReceiver myBroadcast = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("myBroadcast SmsCatcher", "Entered onReceive method");
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("myBroadcast SmsCatcher", "Caught SCREEN_OFF");
        }
        if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("myBroadcast SmsCatcher", "Caught SCREEN_ON");
        }

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Log.i("myBroadcast SmsCatcher", "SMS_RECEIVED");
        }
    }
};

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i("neil SmsCatcher", "Entered onCreate() in 'SmsCatcher extends Service'");
    Toast.makeText(this, "Entered onCreate() in 'SmsCatcher extends Service'", Toast.LENGTH_LONG).show();

    registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
    registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));

}

@Override
public void onDestroy() {
    super.onDestroy();

    Log.i("neil SmsCatcher", "Entered onDestroy() in 'SmsCatcher extends Service'");
    Toast.makeText(this, "Entered onDestroy() in 'SmsCatcher extends Service'", Toast.LENGTH_LONG).show();
}


}

UPDATE 1: Adding some more information from my ongoing debug: I used the App Internal Broadcasts Monitor to see there is a broadcast when there is a text message received and I dont see anything, very strange. What could be the reason? I have other SMS apps installed (Hangout, AT&T Messages) - these cant suppress the broadcast can they?

UPDATE 2: FOUND PROBLEM BUT DONT KNOW HOW TO SOLVE I uninstalled the Google Hangouts (replaces Talk) app and it WORKS!!! Any solution around this? (should i be opening a separate thread as per stackoverflow rules?)

UPDATE 3: FOUND ROOTCAUSE (with help below of course, thank you) It turns out that because of the new Google Hangouts App, I needed to setPriority(int). I found the solution at Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app

Community
  • 1
  • 1
user1406716
  • 9,565
  • 22
  • 96
  • 151

2 Answers2

3

You need to register the android.provider.Telephony.SMS_RECEIVED intent filter,only if you have registered it you can able to check the intent in the onRecive like

    IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(myBroadcastReceiver, filter);

Update From the question: It turns out Google Hangouts App aborts the sms broadcast as soon as it receives it,So disabling SMS support in Hangouts 2.0 may fix the issue.

insomniac
  • 11,146
  • 6
  • 44
  • 55
  • Thanks, I added the registerReceiver(...) but I still dont catch the Intent. I updated the issue with some more debug, I dont think a broadcast is being generated, strange :S – user1406716 Dec 12 '13 at 05:02
  • I am marking yours as the answer as your way of setting the IntentFilter allowed most easily to setPriority(int) [as per my UPDATE 3 above]. Thanks. – user1406716 Dec 12 '13 at 05:22
  • I'll add it in the answer too for future help – insomniac Dec 12 '13 at 05:29
2

You did not register the intent-filter:

@Override
public void onCreate() {
    // ..
    registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
    registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    registerReceiver(myBroadcast, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); // missing in your code
}

And please check your permissions for android.permission.RECEIVE_SMS in AndroidManifest.xml.

flx
  • 14,146
  • 11
  • 55
  • 70
  • Good catch, thanks to @insomniac below also, but I added registerReceiver(...) but I still do not trigger on that Intent filter. Is it correct to use "android.provider.Telephony.SMS_RECEIVED" or is it supposed to be something else? I already have the permissions in my manifest file: – user1406716 Dec 12 '13 at 04:53
  • I have a lead, please see UPDATE 2 above. Any ideas? – user1406716 Dec 12 '13 at 05:08