Skip to content Skip to sidebar Skip to footer

Get Data From Sent Sms

I try to retrieve the data each time the Android sends SMS. Data form: destination phone number delivery time SMS body Anyone knows how?

Solution 1:

Get sms in sent box.

public List<SmsRep> getOutboxSms()
{
    if(null == context)
    {
        returnnewArrayList<SmsRep>();
    }

    UriuriSms= Uri.parse("content://sms/sent");
    Cursorcursor= 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:

publicstatic List<SmsRep> cursor2SmsArray(Cursor cursor)
    {
        if(null == cursor || 0 == cursor.getCount())
        {
            returnnewArrayList<SmsRep>();
        }

        List<SmsRep> messages = newArrayList<SmsRep>();

        try
        {
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                SmsRepsingleSms=newSmsRep();
                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");
                */StringsmsBody= cursor.getString(cursor.getColumnIndexOrThrow("body"));  //### bodybyte[] bodyBytes = smsBody.getBytes("UTF8");
                singleSms.body = TextUtils.htmlEncode(newString(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:

publicclassSmsRep{
    staticString separator ;

    publicint id;
    publicString address;
    public long timestamp;
    publicint type;
    publicint protocol;
    publicString subject;
    publicString body;
    publicString deviceId;

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


}

Is this what you want ?:)

Solution 2:

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

Solution 3:

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

Solution 4:

Uri uriSMS = Uri.parse("content://sms/sent");

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

    Cursor messagesCursor = getActivity().getContentResolver().query(uriSMS , newString[]{"_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)
        {

        }
    }

Post a Comment for "Get Data From Sent Sms"