获得从发送SMS数据 [英] Get data from sent SMS

查看:92
本文介绍了获得从发送SMS数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了Android发送短信每次检索数据。

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

数据格式:

  1. 目标电话号码
  2. 交货时间
  3. 短信身体

有谁知道怎么样?

推荐答案

在发件箱获取短信。

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

SmsRep的定义:

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

这篇关于获得从发送SMS数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆