0

我有一个从电信接收数据短信的应用程序。在我的代码中,我在 Activity 中注册了动态 Broadcastreceiver。它工作正常,但有时我丢失了一些短信,我不知道为什么。这是我的代码

 IntentFilter mFilter = new IntentFilter(
        "android.intent.action.DATA_SMS_RECEIVED");
 mFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
 mFilter.addDataAuthority("localhost", "my port");
 mFilter.addDataScheme("sms");
 if (smsReceiver == null)
     smsReceiver = new SmsReceiverBroadcast(); 
 context.registerReceiver(smsReceiver, intentFilter); 

在将广播注册到活动后,我等待 60 秒

long time = 0;
int delay = 200;
while (!Variable.getMessage && !finish
        && !Util.isTimeout(time, Timer)) {
    Util.sleep(delay);
    time += delay;
}

还有我的广播接收器:

 Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null && bundle.containsKey("pdus")) {

    Object[] pdus = (Object[]) bundle.get("pdus");
    if (pdus != null) {
        String pduString = toHexString((byte[]) pdus[0]);

        LogParams.pdu = (byte[]) pdus[0];
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

        if (msgs[i].getMessageBody() != null) {
            str += msgs[i].getMessageBody().toString();
            Variable.isReadPdu = true;          

        } else {
            PduParser pduparser = new PduParser();
            Pdu pdu = pduparser.parsePdu(pduString);
            str += pdu.getDecodedText();
            Variable.isReadPdu = true;             

        }

我的代码有什么问题?

4

0 回答 0