Wednesday, 26 March 2014

Hidden Android APIs: listening to incoming SMS messages

In the Android platform there are two classes, SmsManager and SmsMessage, for sending SMS messages; hower there is no official class for receiving them. To be able to receive SMS messages we have to use a hidden API, present in the Android platform but not generally available for developers.
Hidden Android APIs contain several constants, methods, interfaces, etc. that can come in handy in a variety of situations.



To discover hidden Android APIs you can use the following resource:
  • AndroidXRef (http://androidxref.com): this site has indexed all the Android source code and offers a useful search box; however you have to know exactly what you are looking for.

Searching class Telephony from AndroidXRef


In order to make use of the Android hidden APIs, there are two possible scenarios:
  • constants: if you just need to use a hidden constant (for example for broacast actions), you can simply copy them from the Android source code into your app. In this tutorial we will use this technique;
  • methods, classes, interfaces, etc.: hidden methods, classes and interfaces must be compiled, before beeing able to use them. To do this you can either modify the SDK jar file used to compile your app, or make use of the java reflection method, each of which has its pros and cons.

To be able to receive incoming SMS messages we will use the following hidden constants contained in the class telephony-common (you can find them using the utilities listed above):
  • android.provider.Telephony.SMS_RECEIVED”: Intent action used in our BroadcastReceiver to listen to incoming SMS messages;
  • pdus”: constant used to retrieve SMS data from the Intent.

Extract from the class Telephony of the Android source code


Extract from the class Telephony of the Android source code


So here is the code:
  1. declare the use of the RECEIVE_SMS permission in the AndroidManifest.xml file;
  2. declare a BroadcastReceiver in the AndroidManifest.xml file with the intent-filter “android.provider.Telephony.SMS_RECEIVED”;
  3. implement the BroadcastReceiver as follows:


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);
   }
  }
 }
}

Once you have a SmsMessage object, you can use one ot its several methods to retrieve useful info about the SMS received:
smsMessage.getOriginatingAddress();
smsMessage.getMessageBody();
etc.

6 comments:

  1. Really very informative and useful post. As a business man this importations are so helpful for me. Due to I am using sms API for my business by a site as experttexting.com those information may help me to understand the system of mobile API.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  4. I think this android SMS API have lots of features thank you for sharing these information here.

    ReplyDelete
  5. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Digital Mobile Marketing
    SMS API
    SMS Marketing

    ReplyDelete
  6. Nice. If you want reach your customer easily just go for sms gateway price india, we provides different kind of Bulk SMS services Without DLT | Free SMS API | Free Registration,
    bulk sms provider hyderabad is one of the most effective, reliable and cost effective ways to establish your businesses. It allows you to get a higher income and get a customer easily. cheapest sms gateway india is the most popular leading company who provides the best service explicit Services.

    You can check sms api pricing.

    ReplyDelete