OnRecieve() from BroadCastReceiver not being called – android 13 (API 33) only.

huangapple go评论61阅读模式
英文:

OnRecieve() from BroadCastReceiver not being called - android 13 (API 33) only

问题

onReceive() 被所有其他 API 级别调用,但出于某种原因,在 API 33 中未被调用。

据我了解,截至 API 31,需要 SCHEDULE_EXACT_ALARM 权限,且自动允许,但在 API 33 之后不再自动设置为 true。

然而,从 API 33 开始,可以使用 USE_ALARM,它从安装时就允许,并且在这种情况下不再需要 SCHEDULE_EXACT_ALARM。

我的权限:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
                 android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

闹钟管理器:

fun schedule(context: Context) {
    val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(context, AlarmReceiver::class.java)
    intent.putExtra("once", once)
    intent.putExtra("vibrate", vibrate)
    intent.putExtra("shake", shake)
    intent.putExtra("snooze", snooze)
    val alarmPendingIntent = PendingIntent.getBroadcast(
        context,
        alarmId, intent,
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )
    val calendar: Calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    calendar.set(Calendar.MINUTE, minute)

    // 如果闹钟时间已过,将日期增加 1 天
    if (calendar.timeInMillis <= System.currentTimeMillis()) {
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1)
    }

    val alarmInfo = AlarmClockInfo(calendar.timeInMillis, alarmPendingIntent)
}

如上所述,这个问题特定于 Android 13。如果我应该在这里发布更具体的信息,请在评论中让我知道,我可以更新。

英文:

onReceive() is called by all other API levels but for some reason is not called by API 33.

As far as I understand as of API 31 SCHEDULE_EXACT_ALARM permission is needed and auto allowed however after API 33 no longer auto set as true.

However as of API 33 USE_ALARM is available which is allowed from install and SCHEDULE_EXACT_ALARM is no longer needed in this case.

my permissions:

&lt;uses-permission android:name=&quot;android.permission.SCHEDULE_EXACT_ALARM&quot;
                 android:maxSdkVersion=&quot;32&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.USE_EXACT_ALARM&quot; /&gt;

alarm manager:

fun schedule(context: Context) {
    val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(context, AlarmReceiver::class.java)
    intent.putExtra(&quot;once&quot;, once)
    intent.putExtra(&quot;vibrate&quot;, vibrate)
    intent.putExtra(&quot;shake&quot;, shake)
    intent.putExtra(&quot;snooze&quot;, snooze)
    val alarmPendingIntent = PendingIntent.getBroadcast(
        context,
        alarmId, intent,
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )
    val calendar: Calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    calendar.set(Calendar.MINUTE, minute)

    //if alarm time has already passed, increment day by 1
    if (calendar.timeInMillis &lt;= System.currentTimeMillis()) {
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1)
    }

    val alarmInfo = AlarmClockInfo(calendar.timeInMillis, alarmPendingIntent)
}

As mentioned above this problem is specific to android 13. If there is something more specific I should post here please let me know in a comment and I can update.

答案1

得分: 1

Android 13 更改了某些权限/清单设置以使其工作,你看了吗?

英文:

https://stackoverflow.com/questions/74146297/android-adding-dynamic-receiver-not-exported-permission-in-release-build

Did you look at how how Android 13 changed some of the permissions / manifest settings for this to work?

答案2

得分: 0

根据Android开发者文档中的AlarmManager部分,有一个方法canScheduleExactAlarms

您尝试过调用该方法并检查是否可以安排精确的闹钟吗?

此外,文档还提到了两点:

针对Build.VERSION_CODES#S或更高版本的应用程序只有在具有Manifest.permission#SCHEDULE_EXACT_ALARM权限或位于设备的省电豁免列表上时才能安排精确的闹钟。

这意味着您应该在清单文件中声明SCHEDULE_EXACT_ALARM权限,看起来您已经这样做了。

这些应用程序还可以启动Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM来从用户那里请求此权限。

注意:应用程序仍然需要在接收到此广播后使用上述API之前检查canScheduleExactAlarms(),因为在应用程序接收到此广播时,权限可能已被撤销。

您还应该从用户那里请求权限以安排精确的闹钟。您是否已经这样做了?

英文:

According to Android Developers Documentation to AlarmManager there is a method canScheduleExactAlarms

Have you tried to call the method and check if you can schedule exact alarms?

Moreover, the documentation also says two things:
> Apps targeting Build.VERSION_CODES#S or higher can schedule exact alarms only if they have the Manifest.permission#SCHEDULE_EXACT_ALARM permission or they are on the device's power-save exemption list.

So it means you should have SCHEDULE_EXACT_ALARM in your manifest, which you seem to declare.

> These apps can also start Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM to request this permission from the user.

> Note: Applications are still required to check canScheduleExactAlarms() before using the above APIs after receiving this broadcast, because it's possible that the permission is already revoked again by the time applications receive this broadcast.

You should also request a permission from the user to schedule exact alarms. Did you do this?

huangapple
  • 本文由 发表于 2023年5月7日 03:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定