本文实例讲述了Android编程判断当前指定App是否在前台的方法。分享给大家供大家参考,具体如下:
//在进程中去寻找当前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程序设计有所帮助。
新闻热点
疑难解答
图片精选