Wednesday 16 April 2014

Hidden Android APIs: hiding SMS messages to the default SMS receiver

In a previous tutorial we saw how to take advantage of the hidden Android APIs to listen to incoming SMS messages.
Now suppose that we want to listen to specific SMS messages (messages coming from a particular number or containing specific strings) and to hide them to the default SMS receiver (like Google Hangout or other client).

This is a private message!

As in the previous tutorial we have to declare a BroadcastReceiver in the AndroidManifest.xml file. However, in order to take priority over the default SMS receiver, we also have to set a high priority for our SMS BroadcastReceiver:
<receiver android:name="com.androidthetechinalblog.SMSReceiver">
   <intent-filter android:priority="9999">
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
   </intent-filter>
</receiver>

Now we just have to make some minor changes to the SMSReceiver class:
public class SMSReceiver extends BroadcastReceiver {
 
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
 
  if(action.equals(“android.provider.Telephony.SMS_RECEIVED”)) {
   Object incomingSMSs[] = (Object[]) intent.getSerializableExtra(“pdus”);
    
   for(Object tmp : incomingSMSs) {
    byte message[] = (byte[]) tmp;
    SmsMessage smsMessage = SmsMessage.createFromPdu(message);

    String phoneNumber = smsMessage.getOriginatingAddress();
    String messageBody = smsMessage.getMessageBody();

    if(phoneNumber.equals("3457148596") || messageBody.contains("test")) {
       //we have found our SMS! here you can perform some actions
       abortBroadcast();
       setResultData(null);
    }
   }
  }
 }
}

As you can see, by calling abortBroadcast() and setResultData(null) (methods working only for "ordered broadcasts", like in our example) we make sure that our SMS won't be propagated to any other receiver.

7 comments:

  1. Does this still work? Ive read that since the update in 4.4 the SMS permissions changed and now third party apps cant execute the abortBroadcast command.

    ReplyDelete
  2. This doesn't work from kitkat onwards.

    ReplyDelete
  3. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Digital Mobile Marketing
    SMS API
    SMS Marketing

    ReplyDelete
  4. Nowadays API plays a vital role in online marketing .I am using bulk sms services and its really beneficial for fast growth.Bulk SMS API

    ReplyDelete
  5. Thanks for sharing. A Bulk SMS API provider makes it possible for companies to be available and visible to their prospective customers by sending them detailed and promotional messages about their products and services.

    ReplyDelete
  6. Thanks for sharing. A Bulk SMS API provider makes it possible for companies to be available and visible to their prospective customers by sending them detailed and promotional messages about their products and services.

    ReplyDelete
  7. Good content!

    If you are in a search of an established and reliable Bulk SMS API provider, contact Eurofox.

    I too have been taking its services for 2 years and am very satisfied with the rsults.

    ReplyDelete