适用于应用生命周期的Android事件监听器 [英] Android event listener for app lifecycle
问题描述
我正在编写一个Android代码段,以帮助跟踪Android事件作为应用程序开发人员的服务工具.
I am writing an Android code segment to help tracing Android event as a service tool for app developers.
例如,主要的应用程序主体可能只是为了显示"hello world".我的代码将监听应用程序事件,例如onStart()
,onResume()
,onDestroy()
等,并跟踪这些事件.
For example, the main app body can be just to display 'hello world'. My code will listen to the app event, such as onStart()
, onResume()
, onDestroy()
, etc, and keep a trace on these events.
当然,可以将代码直接插入到main活动下.但这意味着我的代码将无处不在.有没有一种方法,我可以创建一个对象(即一个侦听器),并且仅请求应用程序开发人员添加1〜2个线性代码来使用我的代码?
Certainly, the code can be inserted directly under the main activity. But that means my code will be allover the places. Is there a way, I can create an object (i.e., a listener), and only request the app developer to add 1~2 liner to use my code?
推荐答案
For API Level 14 and higher, you can call registerActivityLifecycleCallbacks()
on the Application
to set up a listener to be informed about activity lifecycle events.
这篇关于适用于应用生命周期的Android事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!