简体   繁体   English

检测用户何时将应用置于后台

[英]Detect when user put the app in background

My android app uses an SQLite database. 我的Android应用程序使用SQLite数据库。 A service is triggered every weeks to update data. 每隔一周会触发一项服务以更新数据。 Once the service has downloaded the required data, it then writes data with writeData() into the database but I need the service to wait until the user is not using the app anymore. 服务下载了所需的数据后,便使用writeData()将数据写入数据库,但我需要该服务等待用户不再使用该应用程序。

So I have to call my writeData() function WHEN the app is put in background. 因此,当应用程序置于后台时,我必须调用我的writeData()函数。

Is there a way to do that? 有没有办法做到这一点?

您可以创建一个singelton类,并且每当您的应用打开时都会初始化此singelton,因此每次调用写数据之前,请检查您的singelton是否为null,如果为null,则执行writedata方法;否则,用户正在打开应用

这听起来像onPause()生命周期回调的用例。

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

相关问题 用户从android中的后台删除应用程序时如何检测事件? - How to detect event when user removes app from background in android? 当应用程序在后台时,检测设备上的用户交互 - Detect user interactions on device when the application is in the background 当应用程序被杀死/在后台检测连接变化 - Detect Connectivity Change When App is Killed/In Background 检测 Android 应用何时进入后台 - Detect when Android app is going to background 在 flutter 中卸载应用程序时检测用户 - detect user when uninstall app in flutter 当应用程序在后台运行时,如何检测用户何时粘贴内容? - How to detect when user pastes something when application is running in the background? Cordova - 检测该应用程序是否在后台 - Cordova - detect that app is in the background 用户卸载应用程序时后台会发生什么 - What happens in background when user uninstalls app 当我的应用程序在后台运行时,如何检测导致大量数据使用的原因 - How to detect the cause for HUGE data usage when my app in background 如何检测 android 应用程序何时在后台和前台处理一些代码 - how to detect when android app in background and foreground to handle some code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM