因此,正如我在www.example.com站点中所读到developer.android的,Android 13
在Permission字段中带来了新的变化。
我有一个聊天应用程序,我需要打电话,打开相机,录制音频,读写外部存储器。所以在闪屏我请求这些权限给用户。然而,其中一些没有被授予,但甚至没有系统要求我这些。
下面是我在AndroidManifest.xml
文件中声明的内容
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name=".ChatApplication"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="false"
android:hardwareAccelerated="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/logo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"
tools:replace="android:allowBackup, android:label">
这是我在闪屏活动中看到的内容
class SplashActivity : BaseActivity() {
private val splashViewModel: SplashViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
if (!hasPermissions(this, *REQUIRED_PERMISSIONS))
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, ALL_PERMISSIONS)
else
Handler(Looper.getMainLooper()).postDelayed ({ startUi() }, 1000)
}
private fun hasPermissions(context: Context, vararg permissions: String): Boolean =
permissions.all {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
private fun startUi() {
// navigate to main application
}
private fun finishActivity() {
toast("You must grant all required permissions to continue")
finish()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
ALL_PERMISSIONS -> {
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) startUi()
else finishActivity()
}
}
}
companion object {
const val ALL_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS =
mutableListOf (
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA
).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
add(Manifest.permission.READ_MEDIA_IMAGES)
add(Manifest.permission.READ_MEDIA_VIDEO)
add(Manifest.permission.READ_MEDIA_AUDIO)
add(Manifest.permission.POST_NOTIFICATIONS)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}.toTypedArray()
}
}
当我把断点放到onRequestPermissionsResult
上时,我看到了
我在屏幕上看到的权限提示是针对Camera、Record audio和Phone State的。
我正在Pixel 6 Pro API 33
模拟器中运行应用程序。正确的方法是什么?我如何解决这个问题?
3条答案
按热度按时间iyfamqjs1#
根据日志,您可能丢失了3个权限
在清单中添加这3个权限
为了获得所有上述三个权限只采取了外部存储权限.在android文档下面的链接,只有这三个权限提供
READ_MEDIA_IMAGES
,READ_MEDIA_VIDEO
,READ_MEDIA_AUDIO
而不是使用READ_EXTERNAL_STORAGE
.link :https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions
olmpazwi2#
在Android 13中,您无需请求权限
下面是一个传递权限的代码示例
在Mainfests.xml中添加
然后在你的类或activite中添加:
仅当请求权限permissions()时;
46qrfjad3#
问题是我需要将这3个权限添加到清单文件中。