AppMonitor 可以轻松的监听App的前后台状态变化;Activity的活跃状态变化;设备的开关屏状态变化。
主要功能介绍
- 支持监听App的状态变化(前后台切换)
- 支持监听Activity的状态变化
- 支持监听设备屏幕的状态变化(开关屏、解锁)
引入
Gradle:
- 在Project的 build.gradle 里面添加远程仓库
allprojects {
repositories {
//...
mavenCentral()
}
}
- 在Module的 build.gradle 里面添加引入依赖项
implementation 'com.github.jenly1314:app-monitor:1.0.0'
示例
class App : Application() { companion object{ const val TAG = "App" } override fun onCreate() { super.onCreate() initAppMonitor() } private fun initAppMonitor(){ //初始化 AppMonitor.initialize(this,true) //注册监听 App 状态变化(前台,后台) AppMonitor.registerAppStatusCallback(object: AppMonitor.OnAppStatusCallback{ override fun onAppForeground(activity: Activity) { //App 切换到前台 Log.d(TAG,"onAppForeground(Activity = $activity)") } override fun onAppBackground(activity: Activity) { //App 切换到后台 Log.d(TAG,"onAppBackground(Activity = $activity)") } }) //注册监听 Activity 状态变化 AppMonitor.registerActivityStatusCallback(object: AppMonitor.OnActivityStatusCallback{ override fun onAliveStatusChanged( activity: Activity, isAliveState: Boolean, aliveActivityCount: Int ) { //Activity 的存活状态或数量发生变化 Log.d(TAG,"onAliveStatusChanged(Activity = $activity, isAliveState = $isAliveState, aliveActivityCount = $aliveActivityCount)") } override fun onActiveStatusChanged( activity: Activity, isActiveState: Boolean, activeActivityCount: Int ) { //Activity 的活跃状态或数量发生变化 Log.d(TAG,"onActiveStatusChanged(Activity = $activity, isActiveState = $isActiveState, activeActivityCount = $activeActivityCount)") } }) //注册监听屏幕状态变化(开屏、关屏、解锁) AppMonitor.registerScreenStatusCallback(object : AppMonitor.OnScreenStatusCallback{ override fun onScreenStatusChanged(isScreenOn: Boolean) { //屏幕状态发生变化(开屏或关屏) Log.d(TAG,"onScreenStatusChanged(isScreenOn = $isScreenOn)") } override fun onUserPresent() { //解锁:当设备唤醒后,用户在(解锁键盘消失)时回调 Log.d(TAG,"onUserPresent()") } }) } }