0

I am trying to read sms from my inbox using contentresolver. I can read a sms fro specific number actually i am getting both send and receive sms.

mUri = Uri.parse("content://sms/");
mContentResolver = pContext.getContentResolver();

I used above code and getting all sms from specific number

Please understand my requirements below:

  1. I should get a sms for specific sender which is already received in inbox
  2. At present, am getting all the sms conversation for specific number, instead i want to get only a received SMS from inbox for specific number
  3. When i am getting SMS from inbox its showing from latest to old instead, i want to display the SMS in list from old to latest

Thanks in advance

qtmfld
  • 2,916
  • 2
  • 21
  • 36

1 Answers1

0

you can use this class

public class SmsReceiver extends BroadcastReceiver {

String specificPhoneNumber = "Type Number you want";

public void onReceive(Context context, Intent intent) 
{
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";  

        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; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                String phNum = msgs[i].getOriginatingAddress();  
                str += msgs[i].getMessageBody().toString();
         if (specificPhoneNumber.equals(phNum))

        {
            Uri uri = Uri.parse("content://sms/inbox");

            ContentResolver contentResolver = context.getContentResolver();

            String where = "address="+phNum;
            Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                              null);

            while (cursor.moveToNext()) {

                long thread_id = cursor.getLong(1);
                where = "thread_id="+thread_id;
                Uri thread = Uri.parse("content://sms/inbox");
                context.getContentResolver().delete(thread, where, null);

            }
                    Intent l = new Intent(context,AgAppMenu.class);                  
                    l.putExtra("msg",str);                   
                    l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(l);
                }
            }
        }
 }
}

I hope solved your problem :)

Community
  • 1
  • 1
  • actually, i want to get a received SMS from inbox for specific number. i don't want to get all sms for specific number .. just want to get only received SMS – Manikandan Kuppusamy May 14 '18 at 14:53