Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get the SMS Sender Contact (Person) saved name using "content://sms/inbox"

Tags:

android

sms

In my App i want to retrieve the SMS sender saved Name using below code but it always return null. Please suggest me whats the convenient way to get the sender name.

Uri SMS_INBOX = Uri.parse("content://sms/inbox");
         Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
         android.util.Log.i("COLUMNS", Arrays.toString(c.getColumnNames()));

            try {
                if(c.getCount()>0)
                {
                    while (c.moveToNext()){
                     Log.d("SMSss", "Contact : "+c.getString(2)+"\n"
                         +"msg : "+c.getString(11)+"\n"
                         +"ID : "+c.getString(0)+"\n"
                         +"Person : "+c.getString(3));
                    }
                }
            } catch (Exception e) {

            Log.d("mmmmmmmmm"," "+ e.getStackTrace());
            }

i am using following permission in menifest

 <uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Please suggest me how to get. Thanks in advance.

like image 995
IPL10 Avatar asked Nov 04 '13 10:11

IPL10


2 Answers

By this way you can get the saved contact name from inbox.. call the method getAllSms() to get the details..

 public void getAllSms() {
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(message, null, null, null, null);
    startManagingCursor(c);
    int totalSMS = c.getCount();
    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            Log.d("SMSss",
                    "Contact number : "
                            + c.getString(c
                                    .getColumnIndexOrThrow("address"))
                            + "\n"
                            + "msg : "
                            + c.getColumnIndexOrThrow("body")
                            + "\n"
                            + "ID : "
                            + c.getString(c.getColumnIndexOrThrow("_id"))
                            + "\n"
                            + "Person : "
                            + getContactName(
                                getApplicationContext(),
                                    c.getString(c
                                            .getColumnIndexOrThrow("address"))));

            c.moveToNext();
        }
    }
    c.close();

}

public String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri,
            new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor
                .getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}
like image 78
kalyan pvs Avatar answered Sep 27 '22 23:09

kalyan pvs


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

            Cursor c = getContentResolver().query(uriInbox, null, null, null, null); 


            if (c.moveToFirst()) {
                for (int i = 0; i < c.getCount(); i++) {
                    String name = null;
                    String phone = "";
                    String _id = c.getString(c.getColumnIndexOrThrow("_id"));
                    String thread_id = c.getString(c.getColumnIndexOrThrow("thread_id"));
                    String msg = c.getString(c.getColumnIndexOrThrow("body"));
                    String type = c.getString(c.getColumnIndexOrThrow("type"));
                    String timestamp = c.getString(c.getColumnIndexOrThrow("date"));
                    phone = c.getString(c.getColumnIndexOrThrow("address"));
                    name = Function.getContactbyPhoneNumber(getApplicationContext(), c.getString(c.getColumnIndexOrThrow("address")));


                    c.moveToNext();
                }
            }
            c.close();

Finally your getContactbyPhoneNumber method:

public String getContactbyPhoneNumber(Context c, String phoneNumber) {

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME};
    Cursor cursor = c.getContentResolver().query(uri, projection, null, null, null);

    if (cursor == null) {
        return phoneNumber;
    }else {
        String name = phoneNumber;
        try {

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

        } finally {
            cursor.close();
        }

        return name;
    }
}

Courtesy: http://www.androstock.com/tutorials/create-sms-app-android-android-studio-part-2.html

like image 20
Arifuzzaman Arif Avatar answered Sep 27 '22 22:09

Arifuzzaman Arif