To get SMS conversation from an Android device we will use following code:
ContentResolver contentResolver = getActivity().getContentResolver();
            Uri uri = Uri.parse("content://sms/conversations/");
            Cursor cursor = contentResolver.query(uri, null, null, null, "date" + " COLLATE LOCALIZED DESC");
After getting SMS conversation, we can get sms snippets of a particular conversation by the following code:
/**
 * Get SMS from Device
 * @param cur
 */
public void getSMS(Cursor cursor){
    StringBuilder smsBuilder = new StringBuilder();
    try
    {
        while (cursor.moveToNext()) {
            Uri uriSMSURI = Uri.parse("content://sms");
            String[] projection = new String[] { "_id", "address", "thread_id", "body", "date", "type" };  
            Cursor cur = getActivity().getContentResolver().query(uriSMSURI, projection, "thread_id=?", new String[]{cursor.getString(0)},
                    "date" + " COLLATE LOCALIZED DESC");
            if(cur.moveToNext())
            {
                String address = cur.getString(cur.getColumnIndex("address"));
                String name = null;
                String photoUri = null;
                if(address != null)
                {
                    if (address.length() > 0) {
                        // getting contact by contact number
                        String[] contactData = utility.getContactByNumber(address);
                        if (contactData != null) {
                            name = contactData[0];
                            if (contactData[1] != null)
                                photoUri = contactData[1];
                        }
                    } else
                        address = null;
                }
                String body = cur.getString(cur.getColumnIndexOrThrow("body"));
                int int_Type = cur.getInt(cur.getColumnIndexOrThrow("type"));  
                long longDate = cur.getLong(cur.getColumnIndexOrThrow("date"));  
                String dateString = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault()).format(new Date(longDate));
                smsBuilder.append("[ ");  
                smsBuilder.append(address + ", ");  
                smsBuilder.append(name + ", ");  
                smsBuilder.append(body + ", ");  
                smsBuilder.append(date + ", ");  
                smsBuilder.append(int_Type + ", ");  
                smsBuilder.append(photoUri);  
                smsBuilder.append(" ]\n\n"); 
            }
            cur.close();
        }
    }catch (Exception e) {
    }
    catch (OutOfMemoryError e) {
    }
    cursor.close();
    System.out.println(smsBuilder.toString());
}
/**
 * Get Contact Name by Contact Number
 * @param number
 * @return 
 */
private String[] getContactByNumber(final String number) {
    String[] data = new String[2];
    try {
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));
        Cursor cur = activity.getContentResolver().query(uri,
                new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
                null, null, null);
        if (cur.moveToFirst()) {
            int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            data[0] = cur.getString(nameIdx);
            String contactId = cur.getString(cur
                    .getColumnIndex(PhoneLookup._ID));
            // getting contact image uri of the contac by contact id
            String photoUri = getContactPhotoUri(Long.parseLong(contactId));
            if (photoUri != null)
                data[1] = photoUri;
            else
                data[1] = null;
            cur.close();
            return data;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * Get contact photo URI from contact Id
 * @param contactId
 * @return imagePath
 */
private String getContactPhotoUri(long contactId) {
    Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
            contactId);
    String imagePath = null ;
    if(photoUri != null)
    {
        imagePath = photoUri.toString() ;
    }
    return imagePath;
}
Please share the experiences you had while getting SMS from an Android device as this will help other people who read the post.
                       
                    
0 Comment(s)