本文实例讲述了Android编程判断当前指定App是否在前台的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//在进程中去寻找当前APP的信息,判断是否在前台运行 private boolean isAppOnForeground() { ActivityManager activityManager =(ActivityManager) getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE); String packageName =getApplicationContext().getPackageName(); List<RunningAppProcessInfo>appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null ) return false ; for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true ; } } return false ; } |
希望本文所述对大家Android程序设计有所帮助。