如何从Android数据库中存储的多条短文本中恢复一条长SMS(文本)消息?

时间:2018-11-10 17:06:03

标签: android sms smsmanager contentobserver pdu

我有ContentObserver正在听content://sms/inbox。当我从一个Android模拟器向另一个Android模拟器发送长SMS消息时,此ContentObserver会触发乘法(取决于长SMS消息中的短SMS消息数)。我需要将一条短消息连接到一条长消息中,但是我无法确定这些消息是作为一条长消息的一部分发送的,还是它们是独立的成功短消息。似乎可用的cursor列根本不包含这样的功能:

0 = "_id"
1 = "thread_id"
2 = "address"
3 = "person"
4 = "date"
5 = "date_sent"
6 = "protocol"
7 = "read"
8 = "status"
9 = "type"
10 = "reply_path_present"
11 = "subject"
12 = "body"
13 = "service_center"
14 = "locked"
15 = "sub_id"
16 = "error_code"
17 = "creator"
18 = "seen"

据我所知,有一个way可以通过receiver"pdus"进行所需的串联。这是唯一进行的方法吗?

PS 。我发现真实的{不是仿真程序} Android SMS client不会将长消息保留为一系列短消息。它以storeMessage方法连接短消息,并将它们作为完整的长消息保存在数据库中。因此,问题是为什么Android模拟器SMS客户端与真实的客户端不同 !!

更新SmsObserver类:

public class SMSObserver1 extends ContentObserver {
    private Context context;
    private SmsListener listener;

    public SMSObserver(Context context, Handler handler, SmsListener listener) {
        super(handler);
        this.context = context;
        this.listener = listener;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Uri mUri = Uri.parse("content://sms");
        Cursor mCursor = context.getContentResolver().query(mUri, null, null, null, null);
        if (mCursor != null && mCursor.moveToNext()) {
            SmsEntity entity = null;
            int type = mCursor.getInt(mCursor.getColumnIndex("type"));//now we need to decide SMS message is sent or received
            if (type == 1) //it's received SMS
                entity = getSMS(true);
            else if (type == 2) //it's sent SMS
                entity = getSMS(false);
            mCursor.close();
            if (entity != null)
                listener.addSms(entity);
        }
    }

    private SmsEntity getSMS(boolean isIncoming) {
        SmsEntity entity = null;
        Uri uri = Uri.parse(isIncoming ? "content://sms/inbox" : "content://sms/sent");
        Cursor cursor = context.getContentResolver().query(uri, null,null, null, null);
        if (cursor != null && cursor.moveToNext()) {
            entity = printSms(cursor, isIncoming);
            cursor.close();
        }
        return entity;
    }

    private SmsEntity printSms(Cursor cursor, boolean isIncoming){
        int type = cursor.getInt(cursor.getColumnIndex("type"));
        long msg_id= cursor.getLong(cursor.getColumnIndex("_id"));
        String phone = cursor.getString(cursor.getColumnIndex("address"));
        long dateVal = cursor.getLong(cursor.getColumnIndex("date"));
        String body = cursor.getString(cursor.getColumnIndex("body"));
        Date date = new Date(dateVal);

        String str = (isIncoming ? "Received" : "Sent") + " SMS: \n phone is: " + phone;
        str +="\n SMS type is: " + type;
        str +="\n SMS time stamp is:" + date;
        str +="\n SMS body is: " + body;
        str +="\n id is : " + msg_id;
        Log.v("Debug", str);

        return new SmsEntity(msg_id, dateVal, true, isIncoming, phone, body);
    }
}

注册/注销发生在onResume的{​​{1}} / onPause回调中:

Fragment

0 个答案:

没有答案