Search code examples
androidandroid-intentsmsbroadcastreceiverintentfilter

Unable to catch SMS received with android.provider.Telephony.SMS_RECEIVED


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


Solution

  • 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.