Friday, March 20, 2015

Get incoming message text in android

Lots of applications required a text message verification with a code in order to work. If you are also looking to implement such functionality and want to receive an incoming text message in Android using code than use the below:


Message contains a description as well as arbitrary data object that will be sent to a handlers You can use a BroadcastReceiver to get any incoming msg in android.

Broadcast receiver, which is an android component help us to register for application events. Broadcast receiver simply respond to broadcast message that will show that some application is download by a user on a mobile device and ready to use.

Here are two steps to make BroadcastReceiver work:

1. Creating BroadcastReceiver
2. Registering BroadcastReceiver

Example:

  public class IncomingSms extends BroadcastReceiver {
 // Get the object of SmsManager final SmsManager sms = SmsManager.getDefault();


 public void onReceive(Context context, Intent intent) {
  // Retrieves a map of extended data from the intent.  final Bundle bundle = intent.getExtras();
  try {
   if (bundle != null) {
    final Object[] pdusObj = (Object[]) bundle.get("pdus");
    for (int i = 0; i < pdusObj.length; i++) {
     SmsMessage currentMessage = SmsMessage       .createFromPdu((byte[]) pdusObj[i]);     String phoneNumber = currentMessage       .getDisplayOriginatingAddress();
     String senderNum = phoneNumber; //sender phone number     String message = currentMessage.getDisplayMessageBody(); //msg body              // Toast toast = Toast.makeText(context, "senderNum: "     // + senderNum + ", message: " + message, duration);     // toast.show();
    } // end for loop   } // bundle is null
  } catch (Exception e) {   Log.e("SmsReceiver", "Exception smsReceiver" + e);
  } }
}
Dont forget to add permissions in your manifest.

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design