Android应用未触发权限请求,修改Manifest无效及启动页权限咨询
Hey there! Let's work through this permission issue you're hitting with your Splash Screen. The biggest mistake folks make here is forgetting that Android 6.0+ requires dynamic permission requests for "dangerous" permissions—just adding them to your AndroidManifest.xml only declares them, it doesn't trigger the system prompt. Combine that with your 1-second Splash delay, and it's easy for the permission logic to get lost or skipped entirely.
Here's a step-by-step solution tailored to your setup:
1. Double-Check Your Permission Setup First
First, confirm you've actually declared the permissions in your AndroidManifest.xml—for example, if you need camera access:
<uses-permission android:name="android.permission.CAMERA" />
Keep in mind: Only dangerous permissions (like camera, storage, location, microphone) need dynamic requests. Regular permissions (like internet access) are granted automatically at install time, so you won't see a prompt for those.
2. Rework Your Splash Activity Logic
Your current 1-second delay is probably jumping to MainActivity before the permission request can fire. Instead of delaying first, you need to check permissions first, then handle the delay or request based on the result.
Here's a complete code example (Kotlin, easily adaptable to Java):
class SplashActivity : AppCompatActivity() { // Define the permissions you need to request private val REQUIRED_PERMISSIONS = arrayOf(android.Manifest.permission.CAMERA) private val PERMISSION_REQUEST_CODE = 1001 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // Start with permission check, not the delay checkAndRequestPermissions() } private fun checkAndRequestPermissions() { // Check if all required permissions are already granted val allPermissionsGranted = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED } if (allPermissionsGranted) { // Permissions are good—start the 1-second delay before jumping to MainActivity launchMainActivityAfterDelay() } else { // Some permissions are missing—trigger the system permission prompt ActivityCompat.requestPermissions( this, REQUIRED_PERMISSIONS, PERMISSION_REQUEST_CODE ) } } private fun launchMainActivityAfterDelay() { Handler(Looper.getMainLooper()).postDelayed({ startActivity(Intent(this, MainActivity::class.java)) finish() }, 1000) // Your 1-second delay } // Handle the result of the permission request override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == PERMISSION_REQUEST_CODE) { // Check if all permissions were granted val allGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED } if (allGranted) { launchMainActivityAfterDelay() } else { // User denied permissions—show a message and handle accordingly Toast.makeText(this, "We need these permissions to run the app properly!", Toast.LENGTH_SHORT).show() // Optionally: Close the app or prompt again after a delay Handler(Looper.getMainLooper()).postDelayed({ finish() }, 1500) } } } }
3. Troubleshoot Why Your Original Setup Failed
If you still don't see the permission prompt, check these common pitfalls:
- You're requesting a non-dangerous permission: The system won't show a prompt for these—they're granted automatically. Verify your permission falls into Android's dangerous permissions category.
- Your Splash is a static theme, not an Activity: If you're using a
android:themeto show a static Splash instead of an actual Activity, you can't run permission logic there. Switch to an Activity-based Splash. - Your delay ran before the permission request: If you originally had the delay first, the app would jump to MainActivity before the permission prompt could appear. The code above fixes this by checking permissions first.
Bonus: Handle "Never Ask Again"
If the user denies a permission and checks "Never ask again", future requests won't show a prompt. Add this logic to guide them to app settings:
// Inside onRequestPermissionsResult, after checking allGranted val shouldShowRationale = permissions.any { ActivityCompat.shouldShowRequestPermissionRationale(this, it) } if (!shouldShowRationale) { // User checked "Never ask again"—send them to app settings val settingsIntent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { data = Uri.fromParts("package", packageName, null) } startActivity(settingsIntent) }
内容的提问来源于stack exchange,提问作者Swapnil

