public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = "SMS from "; if (bundle != null) { //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length;> msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); if (i==0) { //---get the sender address/phone number--- str += msgs[i].getOriginatingAddress(); str += ": "; } //---get the message body--- str += msgs[i].getMessageBody().toString(); if(str.contains("abc123")) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire(); Toast.makeText(context, "This is not your PHONE!!!!", Toast.LENGTH_LONG).show(); wl.release(); } } //---stop the SMS message from being broadcasted--- this.abortBroadcast(); //---launch the SMSActivity--- Intent mainActivityIntent = new Intent(context, SMSActivity.class); mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mainActivityIntent); //---send a broadcast intent to update the SMS received in the activity--- Intent broadcastIntent = new Intent(); broadcastIntent.setAction("SMS_RECEIVED_ACTION"); broadcastIntent.putExtra("sms", str); context.sendBroadcast(broadcastIntent); } } public void CancelAlarm(Context context) { Intent intent = new Intent(context, SMSReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(sender); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)