Created
November 16, 2015 09:03
-
-
Save walteranyika/5f8cf01c5705b4af38b2 to your computer and use it in GitHub Desktop.
Broadcast receiver code to read incoming sms
This file contains hidden or 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 void onReceive(Context context, Intent intent){ | |
final SmsManager sms = SmsManager.getDefault(); | |
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