How to set an alarm on Android Q?

后端 未结 1 834
野的像风
野的像风 2021-02-06 04:39

Background

Android Q seems to have plenty of new restrictions, but alarms shouldn\'t be one of them:

https://developer.android.com/guide/components/activities/

相关标签:
1条回答
  • 2021-02-06 05:11

    What's wrong in the code I've made? How come it works on P but not always on Q?

    You are attempting to start an activity from the background. That is banned on Android 10+ for the most part.

    According to the docs, alarms shouldn't be harmed.

    From the material that you quoted, with emphasis added: "The app receives a notification PendingIntent from the system". You are not using notifications. And, therefore, this exception does not apply.

    On Android Q, is there an official way to let alarms work correctly? To open an Activity that will be shown to the user, exactly as an alarm clock app should?

    Use a notification with a full-screen Intent, as is covered in the documentation. If the screen is locked, your activity will be displayed when the notification is raised. If the screen is unlocked, a high-priority ("heads up") notification will be displayed instead. In other words:

    • If the device is not being used, you get what you want

    • If the device is probably being used, the user find out about the event without your taking over the screen, so you do not interfere with whatever the user is doing (e.g., relying on a navigation app while driving)

    0 讨论(0)
提交回复
热议问题