Example usage for android.telephony SmsMessage getDisplayOriginatingAddress

List of usage examples for android.telephony SmsMessage getDisplayOriginatingAddress

Introduction

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

Prototype

public String getDisplayOriginatingAddress() 

Source Link

Document

Returns the originating address, or email from address if this message was from an email gateway.

Usage

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

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

    try {//  w  w w  . ja va  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: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/*from   w w w  . jav a 2s  . 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.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();/* ww w  . j  a  v  a2s  .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.polyvi.xface.extension.messaging.XMessagingExt.java

/**
 * ?json/*w  w  w .j a v a 2 s .c o m*/
 * */
private JSONObject buildSmsJsonObj(SmsMessage msg) throws JSONException {

    JSONObject msgJsonObj = new JSONObject();
    msgJsonObj.put("msgId", "");
    msgJsonObj.put("subject", msg.getPseudoSubject());
    msgJsonObj.put("body", msg.getDisplayMessageBody());
    msgJsonObj.put("destinationAddresses", "");
    msgJsonObj.put("originatingAddress", msg.getDisplayOriginatingAddress());
    msgJsonObj.put("messageType", "SMS");
    boolean isRead = false;
    if (SmsManager.STATUS_ON_ICC_READ == msg.getStatusOnIcc()) {
        isRead = true;
    }
    msgJsonObj.put("isRead", isRead);
    msgJsonObj.put("date", msg.getTimestampMillis());
    return msgJsonObj;
}