如何检测应用程序的关闭并采取措施? (科特林)
how to detect the closing of the app and take acction ? ( kotlin )
我想在应用程序关闭时禁用 WiFi。
我知道使用此行禁用 WiFi 的代码:
wifiManager!!.isWifiEnabled = false
but i don't know how to detect the closing of the app.
您可以查看 this blog post。它比我更详细地描述了如何做。
编辑:
博客 post 的重要部分是:
1 - 创建将由自定义应用程序实现的界面 class:
interface LifecycleDelegate {
fun onAppBackgrounded()
fun onAppForegrounded()
}
2 - 现在我们 class 将要实施 ActivityLifecycleCallbacks 和 ComponentCallbacks2:
class AppLifecycleHandler(
private val lifeCycleDelegate: LifeCycleDelegate
) : Application.ActivityLifecycleCallbacks, ComponentCallbacks2
{
private var appInForeground = false
override fun onActivityResumed(activity: Activity?) {
if (!appInForeground) {
appInForeground = true
lifeCycleDelegate.onAppForegrounded()
}
}
override fun onTrimMemory(level: Int) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
appInForeground = false
lifeCycleDelegate.onAppBackgrounded()
}
}
// stub other methods
}
3 - 我们需要在我们的应用程序中使用该处理程序 class:
class App : Application(), LifeCycleDelegate {
override fun onCreate() {
super.onCreate()
val lifeCycleHandler = AppLifecycleHandler(this)
registerLifecycleHandler(lifeCycleHandler)
}
override fun onAppBackgrounded() {
Log.d("App", "App in background")
}
override fun onAppForegrounded() {
Log.d("App", "App in foreground")
}
private fun registerLifecycleHandler(lifeCycleHandler: AppLifecycleHandler) {
registerActivityLifecycleCallbacks(lifeCycleHandler)
registerComponentCallbacks(lifeCycleHandler)
}
}
这正是生命周期的用途。任何需要完成的清理工作都应该在 onDestroy() 中完成。这是您在 activity 被销毁之前收到的最后一个电话。所以在activity你想禁用wifi的地方你可以这样做:
override func onDestroy() {
super.onDestroy();
wifiManager!!.isWifiEnabled = false;
}
我想在应用程序关闭时禁用 WiFi。 我知道使用此行禁用 WiFi 的代码:
wifiManager!!.isWifiEnabled = false
but i don't know how to detect the closing of the app.
您可以查看 this blog post。它比我更详细地描述了如何做。
编辑:
博客 post 的重要部分是:
1 - 创建将由自定义应用程序实现的界面 class:
interface LifecycleDelegate {
fun onAppBackgrounded()
fun onAppForegrounded()
}
2 - 现在我们 class 将要实施 ActivityLifecycleCallbacks 和 ComponentCallbacks2:
class AppLifecycleHandler(
private val lifeCycleDelegate: LifeCycleDelegate
) : Application.ActivityLifecycleCallbacks, ComponentCallbacks2
{
private var appInForeground = false
override fun onActivityResumed(activity: Activity?) {
if (!appInForeground) {
appInForeground = true
lifeCycleDelegate.onAppForegrounded()
}
}
override fun onTrimMemory(level: Int) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
appInForeground = false
lifeCycleDelegate.onAppBackgrounded()
}
}
// stub other methods
}
3 - 我们需要在我们的应用程序中使用该处理程序 class:
class App : Application(), LifeCycleDelegate {
override fun onCreate() {
super.onCreate()
val lifeCycleHandler = AppLifecycleHandler(this)
registerLifecycleHandler(lifeCycleHandler)
}
override fun onAppBackgrounded() {
Log.d("App", "App in background")
}
override fun onAppForegrounded() {
Log.d("App", "App in foreground")
}
private fun registerLifecycleHandler(lifeCycleHandler: AppLifecycleHandler) {
registerActivityLifecycleCallbacks(lifeCycleHandler)
registerComponentCallbacks(lifeCycleHandler)
}
}
这正是生命周期的用途。任何需要完成的清理工作都应该在 onDestroy() 中完成。这是您在 activity 被销毁之前收到的最后一个电话。所以在activity你想禁用wifi的地方你可以这样做:
override func onDestroy() {
super.onDestroy();
wifiManager!!.isWifiEnabled = false;
}