简体   繁体   English

无论手机状态如何,我如何确保我的应用程序继续工作?

[英]How do I ensure that my app keeps working regardless of phone state?

I'm trying to make an app with some scheduled tasks and an accessibility service logging info behind it.我正在尝试制作一个带有一些计划任务和它背后的辅助功能服务日志信息的应用程序。

The problem I see right now is that my tasks, which I use WorkManager to run the Workers at the time of the event, sometimes they don't run at the exact time I scheduled them to.我现在看到的问题是我的任务,我使用WorkManager在事件发生时运行Workers ,有时它们不会在我安排的确切时间运行。 I understand that this happens when the phone is using resources so Android pushes my task back to when it's free to do it, but how would I do it so that it always runs at the time I tell it to?我知道当手机正在使用资源时会发生这种情况,因此 Android 会将我的任务推回到可以自由执行的时间,但是我该怎么做才能让它始终在我告诉它的时候运行?

I've looked into it and found AlarmManager with the setExactAndAllowWhileIdle property could be of use, but I read in the docs that there's a limit to how many times this can run.我看着了一下,发现AlarmManagersetExactAndAllowWhileIdle财产可能是使用的,但我在这有多少次这样可以运行一个限制文档阅读。 I've also looked into Quartz-Scheduler , but I have no idea whether this will work in these cases.我也研究过Quartz-Scheduler ,但我不知道这是否适用于这些情况。

The accessibility service has stopped working when not using my app for a long time too, and I imagine is the same problem (though not completely sure).长时间不使用我的应用程序时,辅助功能服务也停止工作,我想也是同样的问题(虽然不完全确定)。

Would disabling "Battery Optimisation" for my app help with all of this?为我的应用禁用“电池优化”是否有助于解决所有这些问题? Or what would be a good approach to it?或者什么是一个好的方法?

  1. How "exact" you need it to be?你需要它有多“精确”? If it must be really exact maybe you should consider push notifications.如果它必须非常准确,也许您应该考虑推送通知。 Just have in mind that if the notifications are not with a high priority your work will be handled again by the WorkManager.请记住,如果通知的优先级不高,您的工作将再次由 WorkManager 处理。 But with high priority, the user must interact with the notification otherwise the notification will be stoped.但是对于高优先级,用户必须与通知进行交互,否则通知将被停止。 It will mean that you are using push where you need to just use the WorkManager.这意味着您在只需要使用 WorkManager 的地方使用推送。
  2. Have in mind that with any vendor it will be different.请记住,对于任何供应商,情况都会有所不同。 There is no exact answer.没有确切的答案。
  3. WorkManager is not something that gives less than the other options. WorkManager 提供的选项并不比其他选项少。 In the end, you have some restrictions coming from Android.最后,您有一些来自 Android 的限制。 If you are using JobScheduler and AlarmManager you need to handle them on your own.如果您使用 JobScheduler 和 AlarmManager,您需要自己处理它们。
  4. WorkManager handles the work by using JobScheduler and AlarmManager underneath, but the cool thing is that it has its own DB so you won't be losing the works on app restart. WorkManager 通过在下面使用 JobScheduler 和 AlarmManager 来处理工作,但很酷的是它有自己的数据库,因此您不会在应用程序重新启动时丢失工作。 So basically it is more and not less.所以基本上它是更多而不是更少。
  5. From my own experience I know that on some Chinese device(forgot the name) - the WorkManager and the JobScheduler behaved the same, but with Battery optimization off - the WorkManager behaved better.根据我自己的经验,我知道在某些中文设备上(忘记名称)-WorkManager 和 JobScheduler 的行为相同,但是关闭电池优化后-WorkManager 的行为更好。 Long story short - the more you do the better.长话短说——做得越多越好。 You will handle more vendors.您将处理更多供应商。
  6. And yes - you should check the power buckets info.是的 - 您应该检查电源桶信息。 If you are in a lower power bucket and it depends on how much the user interacts with your app - the less you will be executed.如果您处于较低的功率范围内,这取决于用户与您的应用程序交互的程度 - 您执行的次数越少。 Power optimization - off - will help you.电源优化 - 关闭 - 将为您提供帮助。 https://developer.android.com/topic/performance/power/power-details https://developer.android.com/topic/performance/power/power-details
  7. Try checking the JobScheduler dump(As your work is actually a Job underneath).尝试检查 JobScheduler 转储(因为您的工作实际上是下面的工作)。 It will tell you what you are missing for your work to run.它会告诉你你的工作缺少什么。 Maybe you have been executed too many times - you will see QUOTA not satisfied.也许你被执行了太多次——你会看到 QUOTA 不满意。 And if you are making Network calls you have an amount of time per 24h.如果您正在拨打网络电话,您每 24 小时就有一定的时间。 If you have used it you will see CONNECTIVITY not satisfied.如果您使用过它,您会看到 CONNECTIVITY 不满意。 use: adb shell dumpsys jobscheduler More on it here: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging使用: adb shell dumpsys jobscheduler更多关于这里: https : //developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging
  8. I assume you are familiar with the flex interval: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#flexible_run_intervals我假设您熟悉 flex 间隔: https : //developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#flexible_run_intervals

Also there is a site with a summary of how different vendors work :还有一个网站总结了不同供应商的工作方式

https://dontkillmyapp.com/ https://dontkillmyapp.com/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在努力使我的应用程序具有“保持登录状态”状态。 我该怎么办? - I'm working on having a “Keep me on Logged in” state on my app. How should i do it? 如何让 Firebase 电话身份验证适用于我的应用? - How do I get Firebase Phone Authentication to work for my app? 我对应用程序的要求如何拨打和管理电话? - How i do to my app asks to make and manage phone calls? 如何确保使用Spring Framework在我的应用程序中包含给定的依赖项? - How do I ensure that a given dependency is included in my app with the Spring Framework? 如何防止我的Android应用在每次手机进入睡眠状态时重置? - How do I prevent my Android app from resetting every time my phone sleeps? FragmentTest应用程序无法运行,我的应用程序不断崩溃 - FragmentTest app is not working, my app keeps crashing 如何在实现中确保泛型方法的类型? - How do I ensure the type of a generic method in my implementation? 请帮助我的应用程序不断崩溃我的手机和模拟器 - Please help my app keeps crashing on my phone and Emulator 我正在我的应用程序中构建选项卡布局,在调试时我不断收到此错误,阻止应用程序工作并不断崩溃,我该怎么办?, - I am building a Tab layout in my app, while debugging I keep getting this error, preventing the app from working and keeps crashing, what can I do?, 我的应用程序一直停止。 我该如何解决这个问题? - My App keeps stopping. How can i solve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM