【问题标题】:do not receive New SMS收不到新短信
【发布时间】:2013-06-25 12:33:54
【问题描述】:

当短信发送给我时。在BroatcastReceiver的onReceive中,我调用了readNewSMS()方法:

private MySMS readNewSMS(String phoneNumber) {
    MySMS sms = new MySMS();

    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cursor = getSherlockActivity().managedQuery(uri, null,
            null, null, null);
    cursor.moveToFirst();

        sms.setNumber(cursor.getString(
                cursor.getColumnIndexOrThrow("address")).toString());
        sms.setId(cursor.getString(
                cursor.getColumnIndexOrThrow("_id")).toString());
        sms.setThread_id(cursor.getString(cursor
                .getColumnIndexOrThrow("thread_id")));
        sms.setBody(cursor.getString(
                cursor.getColumnIndexOrThrow("body")).toString());
        sms.setTime(cursor.getString(
                cursor.getColumnIndexOrThrow("date")).toString());
        sms.setType(cursor.getString(
                cursor.getColumnIndexOrThrow("type")).toString());

    return sms;
}

但它返回新短信的先前短信,而不是新短信。如何解决此错误?

【问题讨论】:

    标签: android


    【解决方案1】:

    最简单的方法可能是将您的 BroadcastReceiver 注册到 android.provider.Telephony.SMS_RECEIVED 操作并从收到的意图中获取 SMS 消息:

    protected SmsMessage[] extractSmsMessagesFromIntent(Intent p_intent) {
        //---get the SMS message passed in---
        Bundle bundle = p_intent.getExtras();        
        SmsMessage[] msgs = null;
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i = 0; i < msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            }
        }
        return msgs;
    }
    

    【讨论】:

    • 这件事我知道。但我需要 它的 ID 才能从我的短信列表中删除它。
    • 在这种情况下,广播操作不是您想要的。尝试将 ContentObserver 用于 SMS,如以下问题所述:stackoverflow.com/questions/11281533/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多