Created
April 23, 2015 02:39
-
-
Save sunnycmf/e3e1d30f0ab260d716f2 to your computer and use it in GitHub Desktop.
Android read SMS - http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
String message = currentMessage.getDisplayMessageBody(); | |
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message); | |
// Show Alert | |
int duration = Toast.LENGTH_LONG; | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment