Created
December 21, 2017 06:19
-
-
Save omayib/f37c222cd104477b1bc2bdbfc56d7a1e to your computer and use it in GitHub Desktop.
sms-receiver-receiver
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
| package com.hepicar.smsverification; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.telephony.SmsManager; | |
| import android.telephony.SmsMessage; | |
| import android.util.Log; | |
| import android.widget.Toast; | |
| /** | |
| * Created by omayib on 21/12/17. | |
| */ | |
| public class SmsReceiver extends BroadcastReceiver { | |
| private static final String TAG = "SmsReceiver"; | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| // Retrieves a map of extended data from the intent. | |
| Log.d(TAG, "onReceive() called with: 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