Android 12 Schedule_exact_alarm许可未在新过程中应用
。
我已经将MyApp更新
到Target Android 12,并通过新的Schedule_exact_alarm许可遇到了一些奇怪的行为
中介绍了新的介绍权限的问题以下问题
。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myfirm.myapp">
.... more permissions
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application>
....
</application>
<service
android:name="com.myfirm..services.KernelStartedService"
android:enabled="true"
android:exported="true"
android:process=":KernelStartedService">
<intent-filter>
<action android:name="com.myfirm.service.kernel.api.ANDROID_KERNEL_SERVICE_BINDING_ACTION" />
</intent-filter>
</service>
</manifest>
我在我的 androidManifest.xml > 这意味着在需要时创建了一个新的过程,即应用程序的私有过程,并且服务在该过程中运行。
在启动或绑定此服务之前,我会调用测试如果使用此辅助功能的权限:
private fun canExactAlarmsBeScheduled(): Boolean {
val alarmManager = this.getSystemService(ALARM_SERVICE) as AlarmManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
alarmManager.canScheduleExactAlarms()
} else {
true // below API it can always be scheduled
}
}
在第一个活动的onstart方法中,我会进行测试,如果许可为givien,如果不是,我会挑衅向用户显示对话框他需要允许我的应用程序:
override fun onStart() {
// non relevant code here
if (canExactAlarmsBeScheduled()) {
initApplication() // here the service will be started/ bound
} else {
// start System Settings to enable alarms, coming back to Myapp will
// provoke an onStart again
AlertDialog.Builder(this)
.setMessage(getString(R.string.bootstrap_need_permission_to_schedule_alarms,getString(R.string.app_name)))
.setPositiveButton(getString(R.string.dialog_ok)) { _: DialogInterface, _: Int ->
intent = Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
data = Uri.fromParts(PACKAGE_SCHEME, packageName, null)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(intent)
}
.show()
}
}
}
以上这对MyApp效果很好。
问题:
我确实确保只有在给出确切警报的许可时,上述服务“ kernelstartedservice”才会在新过程中开始。 该服务在中运行的新过程未获得已授予的许可,则
如果以后在此专用过程中运行的类中, Almentmanager.canscheduleexactalarms()。然后它总是返回 false !
问题:
我如何才能接管称为“:kernelstartedservice”的过程的权限schedule_exact_alarm? 对于这个新过程,诸如... ...的其他权限
<uses-permission android:name="android.permission.READ_PHONE_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.SCHEDULE_EXACT_ALARM"/>
为什么在“:kernelstartedservice”中未尊重
据我所知,
,这是Android 12(api31 -app)的错误,当它创建未将此权限应用于新过程的过程时。我期望的是
,如果为应用程序制作授予权限,然后我在单独的过程中启动服务,所有权限将应用于新创建的过程。
在此先感谢您的任何帮助!
Hy guys
Situation
I have been updated MyApp to target Android 12 and ran into some odd behavior with the new SCHEDULE_EXACT_ALARM permission.
I have following problem with the new introduced permission
In my AndroidManifest.xml I declared the permission as following:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myfirm.myapp">
.... more permissions
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application>
....
</application>
<service
android:name="com.myfirm..services.KernelStartedService"
android:enabled="true"
android:exported="true"
android:process=":KernelStartedService">
<intent-filter>
<action android:name="com.myfirm.service.kernel.api.ANDROID_KERNEL_SERVICE_BINDING_ACTION" />
</intent-filter>
</service>
</manifest>
As you can see in the declaration the value of the process attributes starts with a colon (:)
this means a new process, private to the application, is created when it's needed and the service runs in that process.
Before Starting or binding this service I invoke a test if the permission is given with this helper function:
private fun canExactAlarmsBeScheduled(): Boolean {
val alarmManager = this.getSystemService(ALARM_SERVICE) as AlarmManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
alarmManager.canScheduleExactAlarms()
} else {
true // below API it can always be scheduled
}
}
In the onStart Method of the first Activity I then do a test if the permission is givien, if not, I provoke showing a dialog to the user that he needs to give this permission to my app:
override fun onStart() {
// non relevant code here
if (canExactAlarmsBeScheduled()) {
initApplication() // here the service will be started/ bound
} else {
// start System Settings to enable alarms, coming back to Myapp will
// provoke an onStart again
AlertDialog.Builder(this)
.setMessage(getString(R.string.bootstrap_need_permission_to_schedule_alarms,getString(R.string.app_name)))
.setPositiveButton(getString(R.string.dialog_ok)) { _: DialogInterface, _: Int ->
intent = Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
data = Uri.fromParts(PACKAGE_SCHEME, packageName, null)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(intent)
}
.show()
}
}
}
This above is working well for the MyApp.
Problem:
I do ensure that only if the Permission of Scheduling exact alarms is given, the above mentioned Service "KernelStartedService" will be started in a new process.
The already granted permission is not taken by the new process in which the service is running in
If later on in classes that are running in this dedicated Process are testing alarmManager.canScheduleExactAlarms(). Then it ALWAYS is returning false!
Question:
How can I achieve to take over the permission SCHEDULE_EXACT_ALARM also for the process called ":KernelStartedService"?
The other permissions like...
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
...are all respected also for this new process. Why is the declaration
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
not respected in the separate process ":KernelStartedService"
As far as I see, this is a bug of Android 12 (API31 -App) when it creates the Process not applying this permission to the new process.
Expected behavior
I would expect that if the permissions are granted for the app-process, and afterwards I start a service in separate process, that all permissions would be applied to the new created process.
Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在清单中使用以下两行解决了Android 13的问题。
using below two lines in manifest solved the problem for android 13.