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

如何通过registerForActivityResult实现无用户干预发送SMS并获取结果?

实现无用户干预自动发送SMS并监听结果

核心说明

  • 你之前使用的Intent.ACTION_SENDTO是系统设计的唤起用户确认发送的方式,无法绕过用户交互——这是Android的安全机制,防止应用私自发送短信。
  • 要实现无干预自动发送,必须使用SmsManager类,同时需要申请SEND_SMS权限,并且可以结合registerForActivityResult完成权限申请和结果监听。

步骤实现

1. 声明权限

在AndroidManifest.xml中添加权限声明:

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

2. 注册权限申请的Activity Result Launcher

用于动态申请发送短信权限:

val requestSmsPermissionLauncher = registerForActivityResult(
    ActivityResultContracts.RequestPermission()
) { isGranted ->
    if (isGranted) {
        // 权限通过,执行自动发送逻辑
        sendAutoSms("3292100441", "The SMS text")
    } else {
        // 权限拒绝,提示用户
        Toast.makeText(this, "需要授予短信发送权限才能自动发送", Toast.LENGTH_SHORT).show()
    }
}

3. 实现自动发送短信并监听结果

通过SmsManager发送短信,并用广播接收器监听发送结果:

private fun sendAutoSms(phoneNumber: String, message: String) {
    val smsManager = SmsManager.getDefault()
    
    // 创建用于接收发送结果的PendingIntent
    val sentIntent = PendingIntent.getBroadcast(
        this,
        0,
        Intent("SMS_SENT_ACTION"),
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )

    // 注册广播接收器处理发送结果
    val smsSentReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (resultCode) {
                Activity.RESULT_OK -> {
                    // 短信发送成功,处理逻辑
                    Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show()
                }
                SmsManager.RESULT_ERROR_GENERIC_FAILURE -> {
                    Toast.makeText(context, "发送失败:通用错误", Toast.LENGTH_SHORT).show()
                }
                SmsManager.RESULT_ERROR_NO_SERVICE -> {
                    Toast.makeText(context, "发送失败:无网络服务", Toast.LENGTH_SHORT).show()
                }
                SmsManager.RESULT_ERROR_NULL_PDU -> {
                    Toast.makeText(context, "发送失败:PDU数据为空", Toast.LENGTH_SHORT).show()
                }
                SmsManager.RESULT_ERROR_RADIO_OFF -> {
                    Toast.makeText(context, "发送失败:无线电已关闭", Toast.LENGTH_SHORT).show()
                }
            }
            // 注销接收器,避免内存泄漏
            context?.unregisterReceiver(this)
        }
    }
    registerReceiver(smsSentReceiver, IntentFilter("SMS_SENT_ACTION"))

    // 执行发送
    smsManager.sendTextMessage(phoneNumber, null, message, sentIntent, null)
}

4. 触发发送逻辑

在需要发送短信的位置,先检查权限,再发起申请或直接发送:

// 检查权限状态
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) 
    != PackageManager.PERMISSION_GRANTED) {
    // 申请权限
    requestSmsPermissionLauncher.launch(Manifest.permission.SEND_SMS)
} else {
    // 已有权限,直接发送
    sendAutoSms("3292100441", "The SMS text")
}

注意事项

  • Android 12及以上版本,PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,这里使用FLAG_IMMUTABLE即可(无需修改Intent内容)。
  • 广播接收器在接收结果后要及时注销,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Giorgio Torassa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:00:17