项目中接收推送时需判断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)
<span style="white-space:pre"> </span>return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
<span style="white-space:pre"> </span>if (appProcess.processName.equals(packageName)
<span style="white-space:pre"> </span>&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
}
return false;
}