Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

android - Get data from sent SMS

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ?:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...