Android 如何检测我的任何活动是否位于最前面并且对用户可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3136187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:52:00  来源:igfitidea点击:

How to detect if any of my activity is front-most and visible to user?

androidandroid-activity

提问by Pentium10

I would like to launch an intent when any of my activity is visible, otherwise I will put it up as a notification, and will be fired by the user.

我想在我的任何活动可见时启动一个意图,否则我会将其作为通知发布,并且会被用户触发。

To decide this, I need to know if any of my activity is front-most, how do I that?

要确定这一点,我需要知道我的任何活动是否位于最前面,我该怎么做?

采纳答案by mbafford

I don't know that there's a method to get the currently displayed activity, but you could do something combining the Activity Lifecycleand a flag.

我不知道有没有一种方法可以获取当前显示的活动,但是您可以将活动生命周期和标志结合起来做一些事情。

For the flag, if you've extended the Application class, that's probably a decent place to store it. For extending the application class, the top answer to this questionhas info. (d).

对于标志,如果您扩展了 Application 类,那可能是存储它的好地方。为了扩展应用程序类,这个问题最佳答案有信息。(d)。

So probably keep track of the current active activity (or a flag that the activity is visible) in onResume/onPause or onStart/onStop depending on exactly what behavior you want.

因此,可能会根据您想要的行为在 onResume/onPause 或 onStart/onStop 中跟踪当前的活动活动(或活动可见的标志)。

Since you have multiple activities, you'll need a centroid place for storing the flag, which is why the Application makes sense. You can get the custom Application object by casting the application context (e.g. ((MyApplication)getApplicationContext()).isMyActivityActive).

由于您有多个活动,因此您需要一个质心位置来存储标志,这就是应用程序有意义的原因。您可以通过转换应用程序上下文来获取自定义应用程序对象(例如 ((MyApplication)getApplicationContext()).isMyActivityActive)。

You could extend Activity as well to help keep this code clean and contained.

您也可以扩展 Activity 以帮助保持此代码的整洁和包含。



If you're using a service you could bind to the service in every activity in the onStart/onStop (or onResume/onPause). If bound, you're visible.

如果您使用的是服务,则可以在 onStart/onStop(或 onResume/onPause)中的每个活动中绑定到该服务。如果绑定,你是可见的。

回答by Zds

You can ask for the running tasks from ActivityManager:

您可以从 ActivityManager 请求正在运行的任务:

ActivityManager activityManager = (ActivityManager)getContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE));

From API docs:

来自 API 文档:

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.

返回当前正在运行的任务列表,最近的任务按顺序排在第一个和较旧的后面。

So the first item on the list is the frontmost activity.

所以列表中的第一项是最前面的活动。

回答by yanchenko

onResume()called && onPause()not called = visible.
Have a public static Activity currentlyVisible;in your Applicationsubclass that will be updated by your activities (set to the instance in onResume()and nulled in onPause()). Or invent a less ugly variant of a registry.

onResume()称为&&onPause()未调用= 可见。在您的子类中
有一个将被您的活动更新(设置为 in和ed in的实例)。或者发明一个不那么丑陋的注册表变体。public static Activity currentlyVisible;ApplicationonResume()nullonPause()

回答by Grant

You could use onWindowFocusChanged(boolean hasFocus) place this in a superclass of your activities to launch the intent if it has focus.

您可以使用 onWindowFocusChanged(boolean hasFocus) 将其放置在您的活动的超类中以启动具有焦点的意图。

回答by Ankita

Instead of using Activity manager there is a simple trick which you can do through code. If you observe the activity cycle closely, the flow between two activities and foreground to background is as follows. Suppose A and B are two activities.

除了使用活动管理器,还有一个简单的技巧,您可以通过代码来完成。如果仔细观察活动周期,两个活动之间以及前台到后台的流程如下。假设 A 和 B 是两个活动。

When transition from A to B: 1. onPause() of A is called 2. onResume() of B is called 3. onStop() of A is called when B is fully resumed

当从 A 转换到 B 时: 1. 调用 A 的 onPause() 2. 调用 B 的 onResume() 3. 当 B 完全恢复时调用 A 的 onStop()

When app goes into background: 1. onPause() of A is called 2. onStop() of A is called

当应用程序进入后台时: 1. 调用 A 的 onPause() 2. 调用 A 的 onStop()

You can detect your background event by simply putting a flag in activity.

您可以通过简单地在活动中放置一个标志来检测您的背景事件。

Make an abstract activity and extend it from your other activities, so that you wont have to copy paste the code for all other activities wherever you need background event.

制作一个抽象活动并将其从您的其他活动中扩展,这样您就不必在需要后台事件的任何地方复制粘贴所有其他活动的代码。

In abstract activity create flag isAppInBackground.

在抽象活动中创建标志 isAppInBackground。

In onCreate() method: isAppInBackground = false;

在 onCreate() 方法中: isAppInBackground = false;

In onPause() method: isAppInBackground = false;

在 onPause() 方法中: isAppInBackground = false;

In onStop() method: isAppInBackground = true;

在 onStop() 方法中: isAppInBackground = true;

You just to need to check in your onResume() if isAppInBackground is true. n after you check your flag then again set isAppInBackground = false

如果 isAppInBackground 为真,您只需要检查您的 onResume()。n 在你检查你的标志然后再次设置 isAppInBackground = false

For transition between two activities since onSTop() of first will always called after second actvity resumes, flag will never be true and when app is in background, onStop() of activity will be called immediately after onPause and hence the flag will be true when you open the app later on.

对于两个活动之间的转换,因为第一个活动的 onSTOP() 将始终在第二个活动恢复后调用,标志永远不会为真,并且当应用程序处于后台时,活动的 onStop() 将在 onPause 后立即调用,因此该标志将在以下情况下为真你稍后打开应用程序。