美文网首页
Android 前后台主动切换

Android 前后台主动切换

作者: 拉弓的松鼠在旋转 | 来源:发表于2023-01-29 12:09 被阅读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)
                        }
                    }
                }
    

    相关文章

      网友评论

          本文标题:Android 前后台主动切换

          本文链接:https://www.haomeiwen.com/subject/twqphdtx.html