Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查我的应用程序是否已失去焦点?_Java_Android - Fatal编程技术网

Java 如何检查我的应用程序是否已失去焦点?

Java 如何检查我的应用程序是否已失去焦点?,java,android,Java,Android,我对编码还不熟悉,我想知道是否有办法检查应用程序何时失去焦点(从前台转到后台) 是否有类似于onLosefocuslistener的内容 澄清一下,我不希望活动失去焦点。我希望当整个应用程序进入后台时,用户按下“主页”按钮或另一个应用程序进入前台。这不容易实现,但有一些轻量级库可以轻松实现您想要的功能 我正在使用,它工作得非常好。这不容易实现,但是有一些轻量级库可以以一种简单的方式实现您想要的功能 我正在使用,它工作得非常好。您可以在应用程序类中注册活动生命周期回调 见: 然后,您可以简单地

我对编码还不熟悉,我想知道是否有办法检查应用程序何时失去焦点(从前台转到后台)

是否有类似于onLosefocuslistener的内容


澄清一下,我不希望活动失去焦点。我希望当整个应用程序进入后台时,用户按下“主页”按钮或另一个应用程序进入前台。

这不容易实现,但有一些轻量级库可以轻松实现您想要的功能


我正在使用,它工作得非常好。

这不容易实现,但是有一些轻量级库可以以一种简单的方式实现您想要的功能


我正在使用,它工作得非常好。

您可以在
应用程序
类中注册活动生命周期回调

见:


然后,您可以简单地拥有一个计数器,该计数器在活动开始时递增,在活动停止时递减。如果计数器的值为零,则当前前台没有活动。

您可以在
应用程序
类中注册活动生命周期回调

见:

然后,您可以简单地拥有一个计数器,该计数器在活动开始时递增,在活动停止时递减。如果计数器的值为零,则当前前景中没有活动。

Kotlin中的示例:

import java.io.Closeable
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking
import androidx.lifecycle.*

class AppLifecycleService() : LifecycleObserver, Closeable {

    val channel = Channel<Boolean>()

    init {
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onMoveToForeground() {
        runBlocking { channel.send(true) }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onMoveToBackground() {
        runBlocking { channel.send(false) }
    }

    override fun close() {
        ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
        channel.close()
    }
}
import java.io.Closeable
导入kotlinx.coroutines.channels.Channel
导入kotlinx.coroutines.runBlocking
导入androidx.lifecycle*
类AppLifecycleService():LifecycleObserver,可关闭{
val通道=通道()
初始化{
ProcessLifecycleOwner.get().lifecycle.addObserver(此)
}
@onLiFeCycleeEvent(Lifecycle.Event.ON_START)
移动地面上的乐趣(){
runBlocking{channel.send(true)}
}
@onLiFeCycleeEvent(Lifecycle.Event.ON_STOP)
MovetoBackground上的乐趣(){
运行阻塞{channel.send(false)}
}
覆盖乐趣关闭(){
ProcessLifecycleOwner.get().lifecycle.removeObserver(此)
频道关闭()
}
}
在某处实例化该类,并订阅该频道。当你获得或失去注意力时,它会给你发送真/假信息。应用程序完成后,
close()
将引用此类。

Kotlin中的示例:

import java.io.Closeable
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking
import androidx.lifecycle.*

class AppLifecycleService() : LifecycleObserver, Closeable {

    val channel = Channel<Boolean>()

    init {
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onMoveToForeground() {
        runBlocking { channel.send(true) }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onMoveToBackground() {
        runBlocking { channel.send(false) }
    }

    override fun close() {
        ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
        channel.close()
    }
}
import java.io.Closeable
导入kotlinx.coroutines.channels.Channel
导入kotlinx.coroutines.runBlocking
导入androidx.lifecycle*
类AppLifecycleService():LifecycleObserver,可关闭{
val通道=通道()
初始化{
ProcessLifecycleOwner.get().lifecycle.addObserver(此)
}
@onLiFeCycleeEvent(Lifecycle.Event.ON_START)
移动地面上的乐趣(){
runBlocking{channel.send(true)}
}
@onLiFeCycleeEvent(Lifecycle.Event.ON_STOP)
MovetoBackground上的乐趣(){
运行阻塞{channel.send(false)}
}
覆盖乐趣关闭(){
ProcessLifecycleOwner.get().lifecycle.removeObserver(此)
频道关闭()
}
}

在某处实例化该类,并订阅该频道。当你获得或失去注意力时,它会给你发送真/假信息。当你的应用程序完成后,
close()
将引用该类。

查看Android活动生命周期。Android运行时将通知您的代码状态更改,然后您决定如何处理。请参阅Android活动生命周期。Android运行时将通知您的代码状态更改,然后您决定如何处理这些更改。