当前位置:   article > 正文

android 服务切至前台,Android 把后台应用切换到前台

android 拉回前台

1、判断当前应用是否在前台

private fun isForeground(context: Context): Boolean {

val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

val tasks = am.getRunningTasks(1)

if (tasks != null && !tasks.isEmpty()) {

val topActivity = tasks[0].topActivity

if (topActivity.packageName == context.packageName) {

return true

}

}

return false

}

2、把当前应用切换到前台

private fun moveTaskToFront() {

val mAm = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager

//获得当前运行的task

val taskList = mAm.getRunningTasks(100)

for (rti in taskList) {

//找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台

if (rti.topActivity.packageName == context.packageName) {

mAm.moveTaskToFront(rti.id, 0)

return

}

}

}

3、添加权限

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/144843
推荐阅读