Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:15:45+00:00 2026-05-30T16:15:45+00:00

Checkout my code I have been doing some work on an sms application and

  • 0

Checkout my code I have been doing some work on an sms application and I am trying now to match the address that has came with the sms with the number that has been stored in the contacts…

So far I was unable to match the contact and display the name of the sender because on emulator contains only two contacts… It matches the first one and displays its name while on phone it doesnot finds a single contact…

public List<String> getSMS() {

        List<String> sms = new ArrayList<String>();
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,
                null);

        ContentResolver cr = getContentResolver();
        Cursor contactsCur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        Cursor phoneCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                "DISPLAY_NAME = '" + name + "'", null, null);

        Log.v(LOG_TAG, "going in loop");
        if (contactsCur.getCount() > 0) {
            Log.v(LOG_TAG, "inside loop");
            while (contactsCur.moveToNext()) {
                Log.v(LOG_TAG, "cursor moving to next");
                id = contactsCur.getString(contactsCur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                Log.i(LOG_TAG,"this is id: " + id);

                name = contactsCur
                        .getString(contactsCur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Cursor phones = cr.query(Phone.CONTENT_URI, null,
                        Phone.CONTACT_ID + " = " + id, null, null);
                Log.i(LOG_TAG, "This is name "+name);
                while (phones.moveToNext()) 
                {
                     number = phones.getString(phones
                            .getColumnIndex(Phone.NUMBER));
                     String aNumber=number.replaceAll("-", "");
                     Log.i(LOG_TAG,"this is number :"+aNumber);

                    Log.v(LOG_TAG, "getting address and message");

                    while (cur.moveToNext()) {
                        String address = cur.getString(cur
                                .getColumnIndex("address"));
                        if(address.equals(aNumber))
                            address=name;
                        String body = cur.getString(cur
                                .getColumnIndexOrThrow("body"));

                        sms.add("Number: " + address+ " .Message: " + body);
                    }
                    Log.v(LOG_TAG, "closing phone cursor");
                    phoneCur.close();
                }

            }
        }

        return sms;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:15:46+00:00Added an answer on May 30, 2026 at 4:15 pm

    I didn’t completely understood what you coded here.But I got your requirement.So my suggestion is-try this way(I haven’t tried it because of the lack of time for now,but logically it should work):

    public List<String> getSMS() {
    
            List<String> sms = new ArrayList<String>();
            //fetch all inbox sms
            Cursor inboxCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null,null);
    
            if(inboxCursor.getCount()>0)
            {
                inboxCursor.moveToFirst();
                for(int i=0;i<inboxCursor.getCount();i++)
                {
                    //get phone-number of sms
                    String address=inboxCursor.getString(inboxCursor.getColumnIndex("address"));
    
                    //fetch contacts with same phone-number
                    Cursor contactCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.NUMBER+"='"+address+"'",null,null);
    
                    if(contactCursor.getCount()>0)
                    {
                        contactCursor.moveToFirst();
    
                        // fetch the name associated with that number in contacts saved
                        String name=contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        //add number,name,message to list   
                        sms.add("Number: " + address+ " .Name: "+ name +".Message: " + body);
                    }
                    else 
                    {  
                        // for unknown contact
                        // add number,name,message to list                     
                        sms.add("Number: " + address+ " .Name: Unknown .Message: " + body);
                    }
                    inboxCursor.moveToNext();
                }
            }
            return sms;
    }
    

    EDIT :

    To group all sms from each contact:

        List<String> groupSms=new List<String>(); 
        // retrieve all contacts
        Cursor contacts=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,null,null,null);     
        if(contacts.getCount()>0)
        {
            contacts.moveToFirst();
            for(int i=0;i<contacts.getCount();i++)
            {    
                 // get phone number       
                 String phone_number=contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) ;
                 // retrieve sms of phone_number from inbox
                 Cursor sms=getContentResolver().query(Uri.parse("content://sms/inbox"), null, "address='"+phone_number+"'", null,null);
                 if(sms.getCount()>0)
                 {
                      sms.moveToFirst();
                      for(int j=0;j<sms.getCount();j++)
                      {
                          // get sms body
                          String text=sms.getString(sms.getColumnIndex("body"));    
                          // add "phone_number" and "text" to a list
                          groupSms.add(phone_number+":"+text);
                          // move to next sms from phone_number
                          sms.moveToNext();
                      }
                 }
                 //move to next contact
                 contacts.moveToNext();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble trying to checkout the BitSharp source code using SVN.
This problem has been bothering me for sometime now, I have not settled on
I have been trying for days to get my gitolite work with jenkins so
I have been working on some code, which I use git to manage. Earlier,
I have a checkout I'm playing with which has some up and down arrows
I am trying to retrieve the order_id of an order that has just been
How do you ensure, that you can checkout the code into Eclipse or NetBeans
I have been working on a shop that is built in Python on the
I have been trying to set up an svn repository on an ubuntu 11.04
I have a simple 'Buy Now' Google Checkout button on my Django site (very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.