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

Android 8(Oreo)发送短信触发java.lang.SecurityException问题咨询

解决Android 8 (Oreo)发送短信的安全权限异常问题

Hey there! Let's dig into this SMS permission issue you're facing on Android 8—this is a super common gotcha because Oreo made some key changes to how dangerous permissions are handled, even though your code works fine on older versions.

问题根源

Android 8.0 (API level 26) tightened up permission requirements for SMS-related operations. While earlier versions might have let you get away with just declaring the permission in your AndroidManifest.xml, Oreo requires explicit dynamic permission requests for dangerous permissions like SEND_SMS. If you skip this step, you'll hit that security exception every time.

分步解决方案

  1. 确认Manifest权限已声明
    First things first—make sure you've got the required permission in your AndroidManifest.xml (this is still necessary, even with dynamic requests):

    <uses-permission android:name="android.permission.SEND_SMS" />
    
  2. 动态申请权限(关键步骤)
    Before you attempt to send an SMS, you need to check if the app already has the SEND_SMS permission. If not, prompt the user to grant it. Here's how to implement this in your Activity:

    private static final int REQUEST_CODE_SEND_SMS = 101;
    
    // Call this method when you want to send an SMS
    private void attemptSendSMS() {
        // Check if we have the permission
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) 
            != PackageManager.PERMISSION_GRANTED) {
            // Permission not granted—request it from the user
            ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.SEND_SMS},
                REQUEST_CODE_SEND_SMS
            );
        } else {
            // Permission already granted—proceed to send SMS
            sendYourSMS();
        }
    }
    
  3. 处理权限申请结果
    You need to listen for the user's response to the permission request and act accordingly. Override onRequestPermissionsResult in your Activity:

    @Override
    public void onRequestPermissionsResult(
        int requestCode,
        @NonNull String[] permissions,
        @NonNull int[] grantResults
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        if (requestCode == REQUEST_CODE_SEND_SMS) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // User allowed the permission—send the SMS now
                sendYourSMS();
            } else {
                // User denied the permission
                Toast.makeText(this, "无法发送短信:需要授予发送短信权限", Toast.LENGTH_SHORT).show();
                
                // If user checked "Don't ask again", guide them to app settings
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
                    Intent settingsIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri appUri = Uri.fromParts("package", getPackageName(), null);
                    settingsIntent.setData(appUri);
                    startActivity(settingsIntent);
                }
            }
        }
    }
    
  4. 短信发送核心逻辑
    Finally, implement your sendYourSMS() method (this is the code that was throwing the exception before, now it'll run only when permission is granted):

    private void sendYourSMS() {
        String targetPhone = "目标手机号";
        String smsContent = "你的短信内容";
        
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(targetPhone, null, smsContent, null, null);
    }
    

为什么旧版本没问题?

Prior to Android 6.0 (API 23), permissions were granted at install time, so declaring them in the Manifest was enough. Even on 6.0-7.1, some devices had looser permission enforcement, but Oreo clamped down on this—now dynamic requests are mandatory for all dangerous permissions, including SEND_SMS.

内容的提问来源于stack exchange,提问作者Khyati Chitroda

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:30:51