从发送的短信中获取数据

时间:2011-03-27 18:32:22

标签: android sms

每次Android发送短信时我都会尝试检索数据。

数据表格:

  1. 目的地电话号码
  2. 交货时间
  3. 短信正文
  4. 任何人都知道怎么做?

4 个答案:

答案 0 :(得分:5)

在发送框中获取短信。

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;
}

处理已发送框中数据的方法:

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;        
}

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
        }


}

这是你想要的吗?:)

答案 1 :(得分:0)

Android SDK中没有任何内容,抱歉。

答案 2 :(得分:0)

我们会在收到短信时收到通知,但在发送短信时无法收到通知。

答案 3 :(得分:-1)

{{1}}