[英]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.