user3001104
user3001104

Reputation: 55

How to receive sms

I need to write application on android to put all message in INBOX to my application .

my code is correct,but appear number of sender ,but i want name of the sender if the sender save in my phone

this code :

public class SecureMessagesActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setTheme( android.R.style.Theme_Light );
        setContentView(R.layout.main);

        /**
         * You can also register your intent filter here.
         * And here is example how to do this.
         *
         * IntentFilter filter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED" );
         * filter.setPriority( IntentFilter.SYSTEM_HIGH_PRIORITY );
         * registerReceiver( new SmsReceiver(), filter );
        **/
        onClick() ;

    }

    ArrayList<String> smsList = new ArrayList<String>();



    public void onClick() 
    {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);

        int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
        int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );

        if ( indexBody < 0 || !cursor.moveToFirst() ) return;

        smsList.clear();

        do
        {

            String str = cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
            smsList.add( str );
        }
        while( cursor.moveToNext() );


        ListView smsListView = (ListView) findViewById( R.id.SMSList );
        smsListView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, smsList) );


}
} 

Upvotes: 0

Views: 119

Answers (1)

Macrosoft-Dev
Macrosoft-Dev

Reputation: 2185

Use below method to get all saved contacs:

public static ArrayList<ContactWrapper> fillContacts(Context c) {
    ArrayList<ContactWrapper> contacts = new ArrayList<ContactWrapper>();
    try {

        // private Uri uriContact;

        String contactName = null;
        String contactID = null;
        String contactNumber = null;

        ContactWrapper cWrapper;

        // getting contacts ID
        Cursor cursorID = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);

        if (cursorID.moveToFirst()) {
            do {
                contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
                contactName = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                contactNumber = DataUtil.retrieveContactNumber(c, contactID);

                if (contactNumber != null && (!contactNumber.equalsIgnoreCase("")) && (!contactNumber.equalsIgnoreCase(" "))) {
                    cWrapper = new ContactWrapper(contactID, contactName, contactNumber, 0);
                    contacts.add(cWrapper);

                    Log.d(c.getClass().getSimpleName(), "Contact ID: " + contactID);
                }

            } while (cursorID.moveToNext());
        }
        cursorID.close();

    } catch (Exception e) {
        e.printStackTrace();
        Log.e(c.getClass().getSimpleName(), "" + e.getMessage());
    }
    return contacts;
}

And in your method get number from sms and find number in above list if exist then show the name like below :

private boolean contactExistInPhoneList(String number) {
    for (int i = 0; i < phoneListcontacts.size(); i++) {
        if (phoneListcontacts.get(i).getNumber().equalsIgnoreCase(number)) {
            Log.d(this.getClass().getSimpleName(), "Name= "+phoneListcontacts.get(i).getName());
            return true;
        }
    }
    return false;
}

Hope it helps. If it helps you please marked it true or up vote it.

Upvotes: 1

Related Questions