Tuesday, June 4, 2013

How can I read SMS messages from the inbox programmatically in Android?

http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android

How can I read SMS messages from the inbox programmatically in Android?

I want to retrieve the SMS messages from the inbox and display them?
share|edit

3 Answers

It is a trivial process. You can see a good example in the source code SMSPopup
Examine the following methods:
public static SmsMmsMessage getSmsDetails(Context context,
                        long ignoreThreadId, boolean unreadOnly)
public static long findMessageId(Context context, long threadId, long _timestamp, int messageType
public static void setMessageRead(Context context, long messageId, int messageType)
public static void deleteMessage(Context context, long messageId, long threadId, int messageType)
this is the method for reading:
        public static SmsMmsMessage getSmsDetails(Context context,
                        long ignoreThreadId, boolean unreadOnly) {

                String SMS_READ_COLUMN = "read";
                String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null;
                String SORT_ORDER = "date DESC";
                int count = 0;

                //Log.v(WHERE_CONDITION);

                if (ignoreThreadId > 0) {
//                      Log.v("Ignoring sms threadId = " + ignoreThreadId);
                        WHERE_CONDITION += " AND thread_id != " + ignoreThreadId;
                }

                Cursor cursor = context.getContentResolver().query(
                                SMS_INBOX_CONTENT_URI,
                      new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                                WHERE_CONDITION,
                                null,
                                SORT_ORDER);

                if (cursor != null) {
                        try {
                                count = cursor.getCount();
                                if (count > 0) {
                                        cursor.moveToFirst();

//                                      String[] columns = cursor.getColumnNames();
//                                      for (int i=0; i<columns.length; i++) {
//                                              Log.v("columns " + i + ": " + columns[i] + ": "
//                                                              + cursor.getString(i));
//                                      }

                                        long messageId = cursor.getLong(0);
                                        long threadId = cursor.getLong(1);
                                        String address = cursor.getString(2);
                                        long contactId = cursor.getLong(3);
                                        String contactId_string = String.valueOf(contactId);
                                        long timestamp = cursor.getLong(4);

                                        String body = cursor.getString(5);

                                        if (!unreadOnly) {
                                                count = 0;
                                        }

                                        SmsMmsMessage smsMessage = new SmsMmsMessage(
                                                        context, address, contactId_string, body, timestamp,
                                                        threadId, count, messageId, SmsMmsMessage.MESSAGE_TYPE_SMS);

                                        return smsMessage;

                                }
                        } finally {
                                cursor.close();
                        }
                }               
                return null;
        }
share|edit
14
This is not part of the Android SDK. This code makes the incorrect assumption that all devices support this undocumented and unsupported content provider. Google has explicitly indicated that relying upon this is not a good idea: android-developers.blogspot.com/2010/05/…CommonsWare Dec 15 '10 at 16:08
1
@CommonsWare what would be a correct solution for this?Janusz Dec 27 '10 at 14:59
@Janusz: There is no documented and supported means that works across all SMS clients on all devices.CommonsWare Dec 27 '10 at 15:07
3
@CommonsWare that is sad to hear. May have to live with this API then.Janusz Dec 28 '10 at 10:09
4
The code has moved. Searching SmsPopupUtils.java got me a new link to it in google code. In case they move it again or discontinue it completely, here's a backup link - pastebin.com/iPt7MLyMKalEl Jun 3 '12 at 12:06
show 1 more comment
Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox.
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveToFirst();

do{
   String msgData = "";
   for(int idx=0;idx<cursor.getColumnCount();idx++)
   {
       msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
   }
}while(cursor.moveToNext());
Please add READ_SMS permission.
I Hope it helps :)
share|edit
Thank you! You misspelled "getColumnName", else than that it works like a charm. Oh, and if anyone will use this, don't forget to add the permission android.permission.READ_SMS.qwerty Mar 28 '12 at 18:40
Thanks. I modified it :)Suryavel TR Mar 30 '12 at 6:37
2
Does this also use the undocumented api that @CommonsWare specified in his comment to the accepted answer?Krishnabhadra Aug 20 '12 at 5:07
Attention! Don't miss moveToFirst as I did.Alexandr Priymak Apr 23 at 14:38
1
@Krishnabhadra Yes. It uses the undocumented "content://sms/inbox" content provider.paul_sns May 2 at 11:07
show 1 more comment
public List<Sms> getAllSms() {
    List<Sms> lstSms = new ArrayList<Sms>();
    Sms objSms = new Sms();
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = mActivity.getContentResolver();

    Cursor c = cr.query(message, null, null, null, null);
    mActivity.startManagingCursor(c);
    int totalSMS = c.getCount();

    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            objSms = new Sms();
            objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
            objSms.setAddress(c.getString(c
                    .getColumnIndexOrThrow("address")));
            objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
            objSms.setReadState(c.getString(c.getColumnIndex("read")));
            objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
            if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                objSms.setFolderName("inbox");
            } else {
                objSms.setFolderName("sent");
            }

            lstSms.add(objSms);
            c.moveToNext();
        }
    }
    // else {
    // throw new RuntimeException("You have no SMS");
    // }
    c.close();

    return lstSms;
}
Sms class is below:
public class Sms{
private String _id;
private String _address;
private String _msg;
private String _readState; //"0" for have not read sms and "1" for have read sms
private String _time;
private String _folderName;

public String getId(){
return _id;
}
public String getAddress(){
return _address;
}
public String getMsg(){
return _msg;
}
public String getReadState(){
return _readState;
}
public String getTime(){
return _time;
}
public String getFolderName(){
return _folderName;
}


public void setId(String id){
_id = id;
}
public void setAddress(String address){
_address = address;
}
public void setMsg(String msg){
_msg = msg;
}
public void setReadState(String readState){
_readState = readState;
}
public void setTime(String time){
_time = time;
}
public void setFolderName(String folderName){
_folderName = folderName;
}

}
share|edit
where is Sms class that used in this code?mSafdel May 13 at 10:27

No comments:

Post a Comment