You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

短信发送状态广播未接收问题及送达后操作实现咨询

Hey there, let's figure out why your SMS status broadcast isn't coming through and get this fixed step by step!

Core Issue: Wrong Broadcast Manager in Use

You're currently using LocalBroadcastManager to register your receiver, but here's the problem: SMS status broadcasts like ACTION_SENT are system-wide global broadcasts, and LocalBroadcastManager only handles local broadcasts sent within your own app. It can't pick up broadcasts emitted by the Android system—this is the root cause of your issue.

Correct Implementation Approach

You need to use the system's Context.registerReceiver() instead of LocalBroadcastManager. Let's walk through the proper setup, plus key considerations for different Android versions:

Dynamic registration is preferred over static manifest registration because it avoids restrictions on implicit broadcasts introduced in newer Android versions. Update your code like this:

BroadcastReceiver smsStatusReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction();
        
        // Handle SMS sent status
        if (Telephony.Sms.Intents.SMS_SENT_ACTION.equals(action)) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    // SMS sent successfully—run your specific action here
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                case SmsManager.RESULT_ERROR_NULL_PDU:
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    // Handle different send failure scenarios
                    break;
            }
        } 
        // Handle SMS delivered status (note: not all carriers support this)
        else if (Telephony.Sms.Intents.SMS_DELIVERED_ACTION.equals(action)) {
            if (getResultCode() == Activity.RESULT_OK) {
                // SMS delivered to the recipient
            }
        }
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    // Register the receiver for system SMS broadcasts
    IntentFilter filter = new IntentFilter();
    filter.addAction(Telephony.Sms.Intents.SMS_SENT_ACTION);
    filter.addAction(Telephony.Sms.Intents.SMS_DELIVERED_ACTION);
    registerReceiver(smsStatusReceiver, filter);
    
    // Rest of your onCreate code...
}

// Don't forget to unregister to avoid memory leaks!
@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(smsStatusReceiver);
}

2. Set Up PendingIntents Correctly When Sending SMS

When you send the SMS, you need to pass PendingIntents for both sent and delivered statuses to SmsManager:

SmsManager smsManager = SmsManager.getDefault();

// PendingIntent for send status
PendingIntent sentPI = PendingIntent.getBroadcast(
    this, 
    0, 
    new Intent(Telephony.Sms.Intents.SMS_SENT_ACTION), 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// PendingIntent for delivery status (carrier-dependent)
PendingIntent deliveredPI = PendingIntent.getBroadcast(
    this, 
    0, 
    new Intent(Telephony.Sms.Intents.SMS_DELIVERED_ACTION), 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// Send the SMS
smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
Additional Troubleshooting Checks
  • Permissions: Make sure you've added the required permission in AndroidManifest.xml:

    <uses-permission android:name="android.permission.SEND_SMS" />
    

    For Android 6.0 (API 23) and above, you also need to request this permission dynamically before sending SMS—don't skip this step, as missing runtime permissions will block SMS sending entirely.

  • PendingIntent Flags: If your app targets Android 12 (API 31) or higher, you must include either FLAG_IMMUTABLE or FLAG_MUTABLE when creating PendingIntents. Using FLAG_IMMUTABLE is safer for status broadcasts.

  • Delivery Broadcast Compatibility: Keep in mind that not all carriers or devices support the SMS delivered broadcast. If you only miss the delivered status, this is likely the reason—focus on verifying the send status first.

内容的提问来源于stack exchange,提问作者Ahmad Dwaik 'Warlock'

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:58:13