2

Sorry for the inaccurate title, I don't know how to express the concept in english.

I want to listen and store a incoming sms sent by any no-digits id (e.g. "FACEBOOK" or "VODAFONE" etc.

When a normal composed phone number (as 555-010-101 and similar) send me a sms, I normally listen to it and store it in the phone db.

Well, it seems like I can't do the same for those special ids.

My receiver is like many others snippets found around the web:

@Override
public void onReceive(Context context, Intent intent) {

    message = "";
    contactName = "";

    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    if ( extras != null ) {

        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

        // Get ContentResolver object for pushing encrypted SMS to incoming folder
        ContentResolver contentResolver = context.getContentResolver();

        assert smsExtra != null;
        for (Object aSmsExtra : smsExtra) {
            sms = SmsMessage.createFromPdu((byte[]) aSmsExtra);

            String body = sms.getMessageBody();
            address = sms.getOriginatingAddress();

            message = body;

            Uri uriConversation = Uri.parse("content://mms-sms/conversations/");
            final String[] projection = new String[]{"*"};
            final String selection = address;

            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
            Cursor cursor = contentResolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

            // THREAD _ ID CURSOR
            Cursor thread_id_cursor = contentResolver.query(
                    uriConversation,
                    projection,
                    selection,
                    null,
                    null
            );

            if (thread_id_cursor.moveToFirst()) {
                thread_id = thread_id_cursor.getString(thread_id_cursor.getColumnIndexOrThrow(THREAD_ID));
            }

            if (cursor.moveToFirst()) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            }

            if (contactName != null && contactName.equals("")) {
                contactName = address;
            }

            entireMessageText.append(sms.getDisplayMessageBody());

        }

        // Put the sms to database
        putSmsToDatabase(contentResolver, sms, entireMessageText.toString());

        NotificationHandler handler = new NotificationHandler(context);
        handler.build();

    }
}

I think I'm missing something for the special ids. Any suggestion would be really appreciated, thanks in advance.

Lampione
  • 1,622
  • 3
  • 21
  • 39

1 Answers1

0

i use this code and work fine check it

@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs;
            String msg_from;
            if (bundle != null) {

                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for (int i = 0; i < msgs.length; i++) {
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();



                        if (// Check msg_from ) {
                            // Do Your Action Hear 
                        }

                        Log.i("SMS Reciver", msgBody + "====>" + msg_from);
                    }
                } catch (Exception e) {
                    Log.d("Exception caught", e.getMessage());
                }

            }
        }


    }
Saeed Darvish
  • 621
  • 6
  • 29
  • Thanks! Your code is pretty much the same as mine. Are you correctly receiving message from special-ids? – Lampione Oct 31 '15 at 16:52
  • Thank you then! I'll test this as soon as possible! – Lampione Oct 31 '15 at 17:26
  • 1
    I've tested it, and unfortunately it's not working. I'm doing test with the facebook approval login code which they send by SMS. Nothing arrive, neither the Log.i get called – Lampione Nov 04 '15 at 08:56
  • any possible solution to achieve this? – mehul9595 Jul 02 '17 at 14:44
  • @mehul9595 unfortunately I gave up on this project a long time ago, since there is really no official support from Android, sadly.. – Lampione Jul 06 '17 at 07:35
  • @Matteo generally apps like whatsapp etc do have capability to automatically fill in received OTP for verification. Is that something not achievable? – mehul9595 Jul 07 '17 at 05:55
  • 1
    @mehul9595 This is possible, yes. – Lampione Jul 07 '17 at 09:42
  • @Matteo: Fixed this adding setting->permissions on Redmi note 4 phone. similarly, https://stackoverflow.com/questions/42120380/cant-read-service-messages-in-redmi-note-3 – mehul9595 Jul 25 '17 at 15:14