简体   繁体   English

检测到我的应用程序小部件的数据不是最新的

[英]Detecting that my app widget's data is not up-to-date

I'm writing an Android app widget that updates every minute with the current data. 我正在编写一个Android应用小部件,该小部件每分钟都会更新一次当前数据。 To update it, I'm using an AlarmManager that only works while the phone is not asleep: 要更新它,我使用的AlarmManager仅在手机未入睡时才起作用:

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), INTERVAL_SEC * 1000, createFrequentUpdateIntent(context));

However, I'm facing the following challenge: when the phone wakes up after a long time, I want the user to see immediately that the data is obsolete because it hasn't been updated for hours. 但是,我面临以下挑战:很长一段时间后手机醒来时,我希望用户立即看到数据已过时,因为它已经有数小时没有更新了。 So I want to somehow mark the data as obsolete if there have been no updates for a long time. 因此,如果长时间没有更新,我想以某种方式将数据标记为过时。

I thought about hooking on the SCREEN_ON event, but apparently this is only possible through a background service and this is strongly discouraged ( here and here ). 我想到了钩在SCREEN_ON事件,但显然这是唯一可能通过后台服务,这是强烈反对( 这里这里 )。

Do you have any ideas of how to do this? 您对此有任何想法吗?

Thanks! 谢谢!

Perhaps in addition to the AlarmManager you could use the typical updateTimeMills update method, and have it called every 30m or so. 也许除了AlarmManager之外,您还可以使用典型的updateTimeMills更新方法,并每30m左右调用一次。 When you get an update from this method, you could replace the widget with the "stale data" version of your layout, such that when the phone comes back on it's already been redrawn with the stale data notification. 当您通过此方法获得更新时,可以将小部件替换为布局的“陈旧数据”版本,这样,当手机重新开机时,已经使用陈旧数据通知重绘了该小部件。

In the event that this update triggers when your phone is on, you could just check for how recent the last update was, and then ignore it if the data is not stale. 如果在手机开机时触发此更新,则可以只检查最近一次更新的时间,如果数据不是陈旧的,则可以忽略它。

I think that this would work because updateTimeMills update is called even when the phone is asleep, so it should still update when your AlarmManager is stopped. 我认为这会起作用,因为即使手机在睡眠状态下也会调用updateTimeMills更新,因此在AlarmManager停止时仍应更新。

暂无
暂无

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

相关问题 Android Studio 在我的应用程序上发出警告:app:stripDebugDebugSymbols UP-TO-DATE - Android Studio Warning on my app: app:stripDebugDebugSymbols UP-TO-DATE 依赖关系是最新的,但Gradle认为不是 - Dependencies are up-to-date but Gradle thinks not 离子无法在最新检查期间捕获任务“:app:preDebugBuild”属性“compileManifests”的输入文件的指纹 - Ionic Failed to capture fingerprint of input files for task ':app:preDebugBuild' property 'compileManifests' during up-to-date check 我无法解决错误“Task:app:writeDebugSigningConfigVersions UP-TO-DATE” - I am having trouble to solve the error "Task :app:writeDebugSigningConfigVersions UP-TO-DATE" 在 Android 中创建闹钟/计时器应用程序的最新方法是什么? - What is the most up-to-date way to create an alarm clock/timer app in Android? 在后台工作期间处理配置更改的正确,最新的方法是什么? - What's the proper, most up-to-date way to handle configuration changes during background work? 获取最新的GPS位置 - Get up-to-date GPS Location 在最新检查期间无法捕获任务':app:preDebugBuild'属性'runtimeManifests'的输入文件的快照 - Failed to capture snapshot of input files for task ':app:preDebugBuild' property 'runtimeManifests' during up-to-date check 在最新检查期间,无法捕获任务':app:transformClassesWithDexBuilderForDebug'属性'$ 1'的输入文件的快照。 - Failed to capture snapshot of input files for task ':app:transformClassesWithDexBuilderForDebug' property '$1' during up-to-date check. Android Studio 2.1.1,完全最新,IDE刚刚崩溃,我的项目文件消失了 - Android Studio 2.1.1, fully up-to-date, IDE just crashed, and my project files disappeared
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM