Android 前后台主动切换
2023-01-29 本文已影响0人
拉弓的松鼠在旋转
1.前台切换到后台
activity.moveTaskToBack(true)
activity.overridePendingTransition(R.anim.slide_right_in, R.anim.empyt_anim)
R.anim.slide_right_in
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromXDelta="100.0%p"
android:toXDelta="0.0%p" />
</set>
R.anim.empyt_anim
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="0.0%p"
android:toXDelta="0.0%p" />
</set>
2.后台切换到前台(比较笨的方法,但是可行)
val manager = activity.getSystemService(Service.ACTIVITY_SERVICE) as ActivityManager
AppStateComponent.topActivity?.let {
manager.moveTaskToFront(it.taskId, 0)
checkTaskDisposable = Observable.intervalRange(1, 5, 0, 1, TimeUnit.SECONDS).subscribe { aLong ->
if (AppStateComponent.state.value == AppStateComponent.STATE_FOREGROUND) {
checkTaskDisposable?.dispose()
} else {
manager.moveTaskToFront(it.taskId, 0)
}
}
}