简体   繁体   English

Android:检测其他活动何时启动(或您的活动失去焦点)

[英]Android: Detect when another Activity is launched (or your activity loses focus)

Like the title says, I need to detect when my app loses focus because another app is launched (Phone call comes in, or user hits Home etc). 就像标题所说,我需要检测我的应用程序何时失去焦点,因为启动了另一个应用程序(电话呼入,或用户点击主页等)。

Overriding Activity.OnStop does not work because that is called even when switching activities within my app. 覆盖Activity.OnStop不起作用,因为即使在我的应用程序中切换活动时也会调用它。

I believe you could use: 我相信你可以使用:

onWindowsFocusChanged(boolean hasFocus) onWindowsFocusChanged(boolean hasFocus)

from your Activity. 来自你的活动。

AFAIK Android offers no facility for this. AFAIK Android不提供此功能。 You may be able to track this yourself (eg, if onStop() in one of your activities is called, and onStart() in another of your activities is not called within X period of time, presumably some other app's activity is in the foreground). 你可以自己跟踪这个(例如,如果你的某个活动中的onStop()被调用,而你的另一个活动中的onStart()在X时间段内没有被调用,可能是其他应用程序的活动在前台)。

With ICS upwards this may be possible. 随着ICS向上,这可能是可能的。

This is taken from the android site: 这是从android站点获取的:

To be notified when the user exits your UI, implement the onTrimMemory() callback in your Activity classes. 要在用户退出UI时收到通知,请在Activity类中实现onTrimMemory()回调。 You should use this method to listen for the TRIM_MEMORY_UI_HIDDEN level, which indicates your UI is now hidden from view and you should free resources that only your UI uses. 您应该使用此方法来侦听TRIM_MEMORY_UI_HIDDEN级别,这表示您的UI现在从视图中隐藏,您应该释放只有您的UI使用的资源。

Notice that your app receives the onTrimMemory() callback with TRIM_MEMORY_UI_HIDDEN only when all the UI components of your app process become hidden from the user. 请注意,只有当应用程序进程的所有UI组件对用户隐藏时,您的应用才会收到带有TRIM_MEMORY_UI_HIDDEN的onTrimMemory()回调。

See this page for full details http://developer.android.com/training/articles/memory.html 有关详细信息,请参阅此页面http://developer.android.com/training/articles/memory.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM