修复闪退问题:权限被拒绝时显示友好对话框而不是立即关闭应用
This commit is contained in:
@@ -3,6 +3,7 @@ package com.example.app
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.ContentValues
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
@@ -14,8 +15,10 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import android.widget.*
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.camera.core.CameraSelector
|
||||
import androidx.camera.core.ImageCapture
|
||||
@@ -600,9 +603,29 @@ class CameraActivity : AppCompatActivity() {
|
||||
startCamera()
|
||||
} else {
|
||||
Toast.makeText(this, "相机权限被拒绝,应用无法使用相机功能", Toast.LENGTH_LONG).show()
|
||||
// 可以选择关闭应用或提供替代功能
|
||||
finish()
|
||||
// 显示友好的界面而不是立即关闭应用
|
||||
showPermissionDeniedScreen()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showPermissionDeniedScreen() {
|
||||
// 可以在这里显示一个友好的界面,解释需要权限的原因
|
||||
// 并提供重新请求权限或退出的选项
|
||||
val dialog = AlertDialog.Builder(this)
|
||||
.setTitle("需要相机权限")
|
||||
.setMessage("此应用需要相机权限才能拍照。请前往设置授予权限。")
|
||||
.setPositiveButton("去设置") { _, _ ->
|
||||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||
intent.data = Uri.parse("package:$packageName")
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
.setNegativeButton("退出") { _, _ ->
|
||||
finish()
|
||||
}
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user