1

I try to retrieve the data each time the Android sends SMS.

Data form:

  1. destination phone number
  2. delivery time
  3. SMS body

Anyone knows how?

Olli
  • 1,231
  • 15
  • 31
Billy
  • 23
  • 1
  • 4
  • what if I use sms manager. i heard that sms manager can catch data from send sms. – Billy Mar 27 '11 at 18:41
  • First, do not answer your own question, unless it is an answer. If you have comments on an answer, add a comment to the answer. Second, `SmsManager` does not "catch data from send sms". – CommonsWare Mar 27 '11 at 19:22
  • there are suggestions for getting the data? Btw, how to reply people answer? i dont see any reply.. – Billy Mar 27 '11 at 19:56
  • "there are suggestions for getting the data?" -- as I wrote, there is nothing in the Android SDK for this. "Btw, how to reply people answer? i dont see any reply" -- you just replied. – CommonsWare Mar 27 '11 at 20:04
  • Your accounts have been merged. You can now add comments – SLaks Mar 27 '11 at 20:33

4 Answers4

6

Get sms in sent box.

public List<SmsRep> getOutboxSms()
{
    if(null == context)
    {
        return new ArrayList<SmsRep>();
    }

    Uri uriSms = Uri.parse("content://sms/sent");
    Cursor cursor = context.getContentResolver().query(uriSms, null,null,null,null); 
    List<SmsRep> outboxSms = cursor2SmsArray(cursor);

    if(!cursor.isClosed())
    {
        cursor.close();
    }

    return outboxSms;
}

And the method to handle data in sent box:

public static List<SmsRep> cursor2SmsArray(Cursor cursor)
    {
        if(null == cursor || 0 == cursor.getCount())
        {
            return new ArrayList<SmsRep>();
        }

        List<SmsRep> messages = new ArrayList<SmsRep>();

        try
        {
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                SmsRep singleSms = new SmsRep();
                singleSms.id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
                singleSms.address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
                singleSms.timestamp = cursor.getLong(cursor.getColumnIndexOrThrow("date")) / 1000;   //### the sent time
                singleSms.type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                singleSms.protocol = cursor.getInt(cursor.getColumnIndexOrThrow("protocol"));

                /*
                String smsSubject = cursor.getString(cursor.getColumnIndex("subject"));
                byte[] subjByts = smsSubject.getBytes("UTF8");
                singleSms.subject = new String(subjByts, "UTF8");
                */


                String smsBody = cursor.getString(cursor.getColumnIndexOrThrow("body"));  //### body
                byte[] bodyBytes = smsBody.getBytes("UTF8");
                singleSms.body = TextUtils.htmlEncode(new String(bodyBytes, "UTF8"));  //escape,handle '='              
                singleSms.deviceId = deviceId;

                //singleSms.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                messages.add(singleSms);
            }

        }
        catch (Exception e) 
        {
            Log.e(TAG, e.getMessage());
        } 
        finally 
        {
            cursor.close();
        }

        return messages;        
}

Definition of SmsRep:

    public class SmsRep 
    {
    static String separator ;

    public int id;
    public String address;
    public long timestamp;
    public int type;
    public int protocol;
    public String subject;
    public String body;
    public String deviceId;

        public SmsRep()
        {
              // do nothing in ctor
        }


}

Is this what you want ?:)

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
qiuping345
  • 148
  • 9
0

There is nothing in the Android SDK for this, sorry.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

We can get notified when you receive a SMS, but there is no way to get notified when a SMS is sent.

Jonas Schmid
  • 5,360
  • 6
  • 38
  • 60
-1
Uri uriSMS = Uri.parse("content://sms/sent");

    Cursor cur = getActivity().getContentResolver().query(uriSMS , null, null, null, null);

    Cursor messagesCursor = getActivity().getContentResolver().query(uriSMS , new String[]{"_id", "address", "body", "person", "date",}, null, null, null);


    if(messagesCursor.getCount() > 0) {
        try {
            while (messagesCursor.moveToNext())
            {
                int x=messagesCursor.getInt(messagesCursor.getColumnIndex("_id"));
                String address=messagesCursor.getString(messagesCursor.getColumnIndex("address"));
                String body=messagesCursor.getString(messagesCursor.getColumnIndex("body"));
                String person=messagesCursor.getString(messagesCursor.getColumnIndex("person"));
                String date_=messagesCursor.getString(messagesCursor.getColumnIndex("date"));

            }
        }
        catch(Exception ex)
        {

        }
    }
Rahul K A
  • 322
  • 4
  • 4