英文:
How to refresh activity while not using the app?
问题
我正在使用实时数据库,即使没有人使用该应用程序,我也希望我的活动能够运行。
swipeRefresh.setOnRefreshListener {
// 代码
binding.swipeRefresh.isRefreshing = false
}
从上面的代码中可以看出,用户需要滑动以进行刷新,我希望它在后台也是相同的,并且可以自动执行。
这意味着该活动每隔五秒钟在后台执行一次,就像是在后台进行一次“刷新”操作。
英文:
I'm working with realtime database where I want my activities to run even no one is using the app.
swipeRefresh.setOnRefreshListener {
// code
binding.swipeRefresh.isRefreshing = false
}
From the above code, the user needs to swipe to refresh, I want it to be the same but in background and it does itself.
That means the activity is like a 'refresh' in the background every five seconds.
答案1
得分: 1
以下是翻译好的内容:
无法在应用程序未被使用时保持其运行。当用户不再使用应用时,Android 应用程序进程将被 Android 终止,无法阻止这种情况发生。Android 这样做是为了节省资源,并在其他应用程序在使用时允许它们运行。我建议阅读文档以了解其工作原理。
相反,当应用程序启动并重新构建用户界面时,您的应用程序应当直接从数据库查询数据。
英文:
It's not possible to keep an app running while it's not being used. Android app processes will be killed by Android when the user is no longer using the app, and this can't be prevented. Android does this to save resources and allow other apps to run when they are being used. I suggest reading the documentation to understand how it works.
Your app should isntead simply query the database again when it's launched and rebuild the UI.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论