有没有可能从android SmsMessage获取userDataHeader?

时间:2016-01-31 18:52:46

标签: android sms

有兴趣从android SmsMessage对象中的userDataHeader获取少量字段(msgCount,refNumber和seqNumber)。有谁知道这是否可能?

object structure tree

1 个答案:

答案 0 :(得分:0)

这些字段在私有/隐藏类中大约有三个级别。你可以用反射来获得它们,但你真的需要吗? msgCount将等于PDU阵列的长度。 PDU是有序的,因此为每个PDU计算seqNumber是微不足道的。而且我无法想出您需要refNumber的任何理由,内部和后端会使用private void listMessageConcatRef(SmsMessage msg) { try { Field msgBaseField = SmsMessage.class.getDeclaredField("mWrappedSmsMessage"); Object msgBase = msgBaseField.get(msg); Class<?> msgBaseCls = Class.forName( "com.android.internal.telephony.SmsMessageBase"); Field dataHeaderField = msgBaseCls.getDeclaredField("mUserDataHeader"); dataHeaderField.setAccessible(true); Object dataHeader = dataHeaderField.get(msgBase); if (dataHeader == null) { Log.d("SmsMessage", "null data header"); return; } Class<?> headerCls = Class.forName( "com.android.internal.telephony.SmsHeader"); Field concatRefField = headerCls.getDeclaredField("concatRef"); Object concatRef = concatRefField.get(dataHeader); Class<?> concatRefCls = Class.forName( "com.android.internal.telephony.SmsHeader$ConcatRef"); Field msgCountField = concatRefCls.getDeclaredField("msgCount"); Field refNumberField = concatRefCls.getDeclaredField("refNumber"); Field seqNumberField = concatRefCls.getDeclaredField("seqNumber"); Field isEightBitsField = concatRefCls.getDeclaredField("isEightBits"); int msgCount = msgCountField.get(concatRef); int refNumber = refNumberField.get(concatRef); int seqNumber = seqNumberField.get(concatRef); boolean isEightBits = isEightBitsField.get(concatRef); Log.d("ConcatRef", "msgCount = " + msgCount); Log.d("ConcatRef", "refNumber = " + refNumber); Log.d("ConcatRef", "seqNumber = " + seqNumber); Log.d("ConcatRef", "isEightBits = " + isEightBits); } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } 来跟踪提交成功/失败并发布状态报告。

在任何情况下,以下是您可以用来确认这一点的反射方法。请注意,这些值仅与多部分消息相关。对于单部分消息,数据头将为空。

$query = $this -> db -> get('order_items');
$array = array();
foreach ($query -> result() as $result) {
    $key = $result -> product_id;
    $value = $result -> qty;
    if (!isset($array[$key])) {
        $array[$key] = $value;
    } else if (array_key_exists($key, $array)) {
        $old = $array[$key];
        $array[$key] = $value + $old;
    } 
}
arsort($array);
$newArray = array_slice($array, 0, 5, true);
return $newArray;