Android 8(Oreo)发送短信触发java.lang.SecurityException问题咨询
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.
分步解决方案
确认Manifest权限已声明
First things first—make sure you've got the required permission in yourAndroidManifest.xml(this is still necessary, even with dynamic requests):<uses-permission android:name="android.permission.SEND_SMS" />动态申请权限(关键步骤)
Before you attempt to send an SMS, you need to check if the app already has theSEND_SMSpermission. 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(); } }处理权限申请结果
You need to listen for the user's response to the permission request and act accordingly. OverrideonRequestPermissionsResultin 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); } } } }短信发送核心逻辑
Finally, implement yoursendYourSMS()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

