如何获得回调时,Android应用程序来自于背景的前景?来自于、回调、如何获得、应用程序

2023-09-12 22:21:29 作者:可惜你从未心疼我的笨

如何知道应用程序状态的背景前景的机器人?

How to know app state background to foreground in android?

我从一个Baseactivity调用扩展我的活动和Baseactivity类扩展了Android的活动。 我把code appcomeForeground()为基础的活动上onRestart(),但它的呼叫,当我们浏览活动到我们的前台应用程序还。

I had extends my activities from one Baseactivity call and Baseactivity class extends android Activity. I put code appcomeForeground() into base activity on onRestart() but its call when we navigate activity into our foreground app also.

请建议的方式得到回电,只有当应用程序自带的前景。

Please suggest way to get call back only when app comes foreground.

在此先感谢。

推荐答案

要检查你的应用是否在前台,你可以做以下的背景。

to check whether your application is in background of foreground you can do the following.

声明一个类,将保持状态

Declare a class which will maintain the state

public class ApplicationState {
    public static boolean isActivityVisible() {
        return activityVisible;
    }

    public static void activityResumed() {
        activityVisible = true;
    }

    public static void activityPaused() {
        activityVisible = false;
    }

    private volatile static boolean activityVisible;
}

onResume 您的应用程序调用的每一个活动的方法

in the onResume method of every activity of your application call

ApplicationState.activityResumed()

和在应用程序调用的每一个活动的的onPause 方法

and in onPause method of every activity of your application call

ApplicationState.activityPaused()

现在随时可以通过打电话只是检查应用程序的前台/后台状态

Now at anytime you can check the foreground/background state of your application by just calling

ApplicationState.isActivityVisible()