Example usage for android.telephony SmsMessage createFromPdu

List of usage examples for android.telephony SmsMessage createFromPdu

Introduction

In this page you can find the example usage for android.telephony SmsMessage createFromPdu.

Prototype

@Deprecated
public static SmsMessage createFromPdu(byte[] pdu) 

Source Link

Document

Create an SmsMessage from a raw PDU.

Usage

From source file:in.ceeq.receivers.MobileMessagesReceiver.java

/**
 * Sms commands receiver commands allowed Siren, Ring, Now, Calls,
 *///from   w  ww.  j ava 2 s  . co m
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    String messageText = smsMessage[0].getMessageBody().toString().toUpperCase();
    String senderAddress = smsMessage[0].getOriginatingAddress();
    Utils.setStringPrefs(context, Utils.SENDER_ADDRESS, senderAddress);
    Intent sendCommand = new Intent(context, CommandService.class);
    if (messageText.contains("CEEQ") && messageText.contains(Utils.getStringPrefs(context, Utils.PIN_NUMBER))) {
        if (messageText.contains("ALARM")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.SIREN_ON);
        } else if (messageText.contains("RING")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.RINGER_ON);
        } else if (messageText.contains("ERASE")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.WIPE);
        } else if (messageText.contains("NOW")) {
            sendCommand.putExtra(CommandService.ACTION,
                    CommandService.GET_LOCATION_FOR_CURRENT_DETAILS_MESSAGE);
        } else if (messageText.contains("CALLS")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.SEND_CALLS_DETAILS_MESSAGE);
        } else if (messageText.contains("SPY")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.ENABLE_TRACKER);
        } else if (messageText.contains("LOCATE")) {
            sendCommand.putExtra(CommandService.ACTION, CommandService.GET_LOCATION_FOR_MESSAGE);
        }
    } else
        sendCommand.putExtra(CommandService.ACTION, CommandService.SEND_PIN_FAIL_MESSAGE);

    startWakefulService(context, sendCommand);
    setResultCode(Activity.RESULT_OK);
}

From source file:stream.messagingsample.SmsReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
    String body = "";

    for (int i = 0; i < smsExtra.length; ++i) {
        SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
        body += sms.getMessageBody();// w w w  . ja  va 2 s  .  c om
    }

    Notification notification = new Notification.Builder(context).setContentText(body)
            .setContentTitle("New Message").setSmallIcon(stream.messagingsample.R.drawable.ic_alert)
            .setStyle(new Notification.BigTextStyle().bigText(body)).build();
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(1, notification);
}

From source file:com.klinker.android.messaging_sample.SmsReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
    String body = "";

    for (int i = 0; i < smsExtra.length; ++i) {
        SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
        body += sms.getMessageBody();//from   w  ww  . ja va2 s . c  o  m
    }

    Notification notification = new Notification.Builder(context).setContentText(body)
            .setContentTitle("New Message").setSmallIcon(R.drawable.ic_alert)
            .setStyle(new Notification.BigTextStyle().bigText(body)).build();
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(1, notification);
}

From source file:com.demo.android.smsapp.receivers.SMSReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();

    try {/*from ww  w.j a  v a  2s . c om*/

        if (bundle != null) {
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            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 message = currentMessage.getDisplayMessageBody();

                Intent chatActivityIntent = new Intent(context, SMSChatActivity.class);
                chatActivityIntent.putExtra("number", phoneNumber);
                PendingIntent pi = PendingIntent.getActivity(context, 1, chatActivityIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(phoneNumber).setContentText(message)
                        .setContentIntent(pi)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

                notificationManager.notify(99, mBuilder.build());

                Log.i("SmsReceiver", "senderNum: " + phoneNumber + "; message: " + message);

            }
        }

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}

From source file:org.telegram.messenger.SmsListener.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        if (!AndroidUtilities.isWaitingForSms()) {
            return;
        }/*from  w w  w  .j a  v a 2 s .  co  m*/
        //    PendingIntent.getActivity().startService(new Intent(context, ServiceChatHead.class));

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs;
        if (bundle != null) {
            try {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                String wholeString = "";
                for (int i = 0; i < msgs.length; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    wholeString += msgs[i].getMessageBody();
                }

                try {
                    Pattern pattern = Pattern.compile("[0-9]+");
                    final Matcher matcher = pattern.matcher(wholeString);
                    if (matcher.find()) {
                        String str = matcher.group(0);
                        if (str.length() >= 3) {
                            AndroidUtilities.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    NotificationCenter.getInstance().postNotificationName(
                                            NotificationCenter.didReceiveSmsCode, matcher.group(0));
                                }
                            });
                        }
                    }
                } catch (Throwable e) {
                    FileLog.e("tmessages", e);
                }

            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
        }
    }
}

From source file:ru.glesik.nostrangersms.SMSReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            // Get SMS objects.
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus.length == 0) {
                return;
            }/*from  ww w .j a  v a 2  s . co m*/
            // Large message might be broken into many.
            SmsMessage[] messages = new SmsMessage[pdus.length];
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                sb.append(messages[i].getMessageBody());
            }
            String sender = messages[0].getOriginatingAddress();
            if (!(getContactDisplayNameByNumber(context, sender).equals(""))) {
                // Contact with this number exists in address book: do nothing.
            } else {
                // Contact not in address book: log message and don't let it through.
                String message = sb.toString();
                // Prevent other broadcast receivers from receiving broadcast.
                abortBroadcast();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // ISO 8601, Local time zone.
                dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                String date = dateFormat.format(new Date()); // Current time in UTC.
                DatabaseHandler db = new DatabaseHandler(context);
                db.addSms(new SMSMessage(date, sender, message), 0); // 0 is for no limit.
                // TODO: delete oldest messages
                NotificationManager mNotificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                int icon;
                icon = R.drawable.ic_stat_sad_sms;
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(icon)
                        .setContentTitle(sender).setContentText(message)
                        .setPriority(NotificationCompat.PRIORITY_LOW).setOnlyAlertOnce(true);
                // TODO: Optional light notification.
                Intent ni = new Intent(context, JunkSMSList.class);
                ni.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent pi = PendingIntent.getActivity(context, 0, ni, 0);
                mBuilder.setContentIntent(pi);
                mBuilder.setAutoCancel(true);
                mNotificationManager.notify(777, mBuilder.build());
            }
        }
    }
}

From source file:sg.macbuntu.android.pushcontacts.SmsReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();/*from   ww w.  ja  va  2  s.co  m*/
    SmsMessage[] msgs = null;

    String contact = "";
    String sender = "";
    String body = "";
    String account = "";

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean pushPreference = prefs.getBoolean("cbPush", true);

    if (pushPreference) {
        if (bundle != null && accountExist(context)) {
            Object[] pdus = (Object[]) bundle.get("pdus");

            msgs = new SmsMessage[pdus.length];
            msgs[0] = SmsMessage.createFromPdu((byte[]) pdus[0]);

            contact = msgs[0].getOriginatingAddress();
            body = msgs[0].getMessageBody().toString();
            account = getAccount(context);
            sender = getNameFromPhoneNumber(context, contact);

            Toast.makeText(context, R.string.toast_push, Toast.LENGTH_LONG).show();
            postData(account, contact, body, sender);
        }
    }
}

From source file:com.androzic.plugin.tracker.SMSReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String Sender = "";
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    Log.e(TAG, "SMS received");

    Bundle extras = intent.getExtras();/*  w  w w. j  a v  a 2  s . c om*/
    if (extras == null)
        return;

    StringBuilder messageBuilder = new StringBuilder();
    Object[] pdus = (Object[]) extras.get("pdus");
    for (int i = 0; i < pdus.length; i++) {
        SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String text = msg.getMessageBody();
        Sender = msg.getDisplayOriginatingAddress();
        Log.w(TAG, "Sender: " + Sender);
        if (text == null)
            continue;
        messageBuilder.append(text);
    }

    String text = messageBuilder.toString();
    boolean flexMode = prefs.getBoolean(context.getString(R.string.pref_tracker_use_flex_mode),
            context.getResources().getBoolean(R.bool.def_flex_mode));

    Log.i(TAG, "SMS: " + text);
    Tracker tracker = new Tracker();
    if (!parseXexunTK102(text, tracker) && !parseJointechJT600(text, tracker)
            && !parseTK102Clone1(text, tracker) && !(parseFlexMode(text, tracker) && flexMode))
        return;

    if (tracker.message != null) {
        tracker.message = tracker.message.trim();
        if ("".equals(tracker.message))
            tracker.message = null;
    }

    tracker.sender = Sender;

    if (!"".equals(tracker.sender)) {
        // Save tracker data
        TrackerDataAccess dataAccess = new TrackerDataAccess(context);
        dataAccess.updateTracker(tracker);

        try {
            Application application = Application.getApplication();
            tracker = dataAccess.getTracker(tracker.sender);//get  latest positon of tracker

            application.sendTrackerOnMap(dataAccess, tracker);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dataAccess.close();

        context.sendBroadcast(new Intent(Application.TRACKER_DATE_RECEIVED_BROADCAST));

        // Show notification
        boolean notifications = prefs.getBoolean(context.getString(R.string.pref_tracker_notifications),
                context.getResources().getBoolean(R.bool.def_notifications));
        if (notifications) {
            Intent i = new Intent("com.androzic.COORDINATES_RECEIVED");
            i.putExtra("title", tracker.message != null ? tracker.message : tracker.name);
            i.putExtra("sender", tracker.name);
            i.putExtra("origin", context.getApplicationContext().getPackageName());
            i.putExtra("lat", tracker.latitude);
            i.putExtra("lon", tracker.longitude);

            String msg = context.getString(R.string.notif_text, tracker.name);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle(context.getString(R.string.app_name));
            if (tracker.message != null)
                builder.setContentText(tracker.name + ": " + tracker.message);
            else
                builder.setContentText(msg);
            PendingIntent contentIntent = PendingIntent.getBroadcast(context, (int) tracker._id, i,
                    PendingIntent.FLAG_ONE_SHOT);
            builder.setContentIntent(contentIntent);
            builder.setSmallIcon(R.drawable.ic_stat_tracker);
            builder.setTicker(msg);
            builder.setWhen(tracker.time);
            int defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
            boolean vibrate = prefs.getBoolean(context.getString(R.string.pref_tracker_vibrate),
                    context.getResources().getBoolean(R.bool.def_vibrate));
            if (vibrate)
                defaults |= Notification.DEFAULT_VIBRATE;
            builder.setDefaults(defaults);
            builder.setAutoCancel(true);
            Notification notification = builder.build();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify((int) tracker._id, notification);
        }

        // Conceal SMS
        boolean concealsms = prefs.getBoolean(context.getString(R.string.pref_tracker_concealsms),
                context.getResources().getBoolean(R.bool.def_concealsms));
        if (concealsms)
            abortBroadcast();
    }
}

From source file:com.oodles.smslistener.SmsListenerModule.java

void setupIntentReceivers() {
    Log.d(LCAT, "inside setupIntentReceivers");
    Activity currentActivity = TiApplication.getInstance().getCurrentActivity();

    //let's register broadcast receivers
    BroadcastReceiver smsReceiver = new BroadcastReceiver() {
        @Override/* w w w.  j  av a 2  s  .  c  o  m*/
        public void onReceive(Context context, Intent intent) {
            Log.d(LCAT, "inside onReceive");
            KrollDict event;
            switch (getResultCode()) {
            case Activity.RESULT_OK:

                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();
                            senderNum = phoneNumber;
                            message = currentMessage.getDisplayMessageBody();
                        }
                    }
                } catch (Exception e) {
                    Log.e("SmsReceiver", "Exception smsReceiver" + e);
                }
                event = createEventObject(true, RECEIVED, message, senderNum);
                Log.d(LCAT, "message Received");
                fireEvent("complete", event);
                break;
            default:
                event = createEventObject(false, FAILED, "Message delivery failed", "");
                Log.d(LCAT, "message receiving failure");
                fireEvent("complete", event);
                break;
            }
        }
    };

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.setPriority(1000);
    intentFilter.addAction(MESSAGE_RECEIVED);
    //---when the SMS has been Received---
    currentActivity.registerReceiver(smsReceiver, intentFilter);
}

From source file:com.example.hackinghealthclinification.app.ClinificationApp.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clinification_app);

    initializeLaunchButtons();//from www .  j  a v a2 s .  c om
    mIsRegistered = false; // check if BroadcastReceiver registered
    IntentFilter filterSmsReceived = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

    final SMSConfirmDialog.ConfirmDialogListener mListener = new SMSConfirmDialog.ConfirmDialogListener() {
        @Override
        public void onDialogPositiveClick(SMSConfirmDialog c) {
            //                mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("yes"
            //                        + mPhoneNumber)));
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(mServerNumber, null, "yes", null, null);
        }

        @Override
        public void onDialogNegativeClick(SMSConfirmDialog c) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(mServerNumber, null, "no", null, null);
        }
    };

    mReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            mContext = context;
            Bundle pudsBundle = intent.getExtras();
            Object[] pdus = (Object[]) pudsBundle.get("pdus");
            SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
            Log.i("Error with SMS ::: ", messages.getMessageBody());
            if (messages.getMessageBody().contains(SMS_TRIGGER_KEY)) {
                mPhoneNumber = messages.getOriginatingAddress();
                SMSConfirmDialog dialog = new SMSConfirmDialog(mListener,
                        "Will you accept the new appointment: " + "\n" + messages.getMessageBody(), "Yes",
                        "No");
                dialog.show(getSupportFragmentManager(), null);
            }
        }
    };

    this.registerReceiver(mReceiver, filterSmsReceived);
    mIsRegistered = true;

}