判断程序是否在后台运行工具
2018-03-27 11:47
351 查看
public class AppUtils {
/**
* 判断应用是否在后台
*/
public static boolean isBackgroundRunning() {
try {
Context context = BaseApp.getInstance();
if(context == null) return false; // 防止应用打开时没来得及初始化AppContext
String processName = context.getPackageName();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (activityManager == null) return true;
// get running application processes
List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processList) {
if (process.processName.startsWith(processName)) {
boolean isBackground = process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
boolean isLockedState = keyguardManager.inKeyguardRestrictedInputMode();
// Log.e("AppUtils", "AppUtils isLockedState = "+isLockedState+";isBackground = "+isBackground);
if (isBackground || isLockedState) {
return true;
}else{
return false;
}
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("AppUtils", ""+e);
}
return true;
}
/**判断当前是否存在活动的activity*/
public static boolean isBackRunningTask(){
ActivityManager am = (ActivityManager) BaseApp.getInstance()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(BaseApp.getInstance().getPackageName())) {
return true;
}
}
return false;
}
}
/**
* 判断应用是否在后台
*/
public static boolean isBackgroundRunning() {
try {
Context context = BaseApp.getInstance();
if(context == null) return false; // 防止应用打开时没来得及初始化AppContext
String processName = context.getPackageName();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (activityManager == null) return true;
// get running application processes
List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processList) {
if (process.processName.startsWith(processName)) {
boolean isBackground = process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& process.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
boolean isLockedState = keyguardManager.inKeyguardRestrictedInputMode();
// Log.e("AppUtils", "AppUtils isLockedState = "+isLockedState+";isBackground = "+isBackground);
if (isBackground || isLockedState) {
return true;
}else{
return false;
}
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("AppUtils", ""+e);
}
return true;
}
/**判断当前是否存在活动的activity*/
public static boolean isBackRunningTask(){
ActivityManager am = (ActivityManager) BaseApp.getInstance()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(BaseApp.getInstance().getPackageName())) {
return true;
}
}
return false;
}
}
相关文章推荐
- 判断Android程序是否在后台中运行
- Android之通过ActivityLifecycleCallbacks判断程序是否运行在后台
- android 优雅的判断程序是否在后台运行
- Android 判断app是否运行在后台
- Shell之判断程序是否运行-yellowcong
- 如何判断当前程序是否运行在虚拟机内
- Android判断某个应用程序是否在后台运行?
- 使用sharepreference判断是否是第一次运行程序
- 判断后台service是否在运行
- Android 判断用户是否第一次运行此程序
- C#判断程序是否在运行
- 判断程序是否在运行
- 判断手机操作系统版本是否允许运行程序
- C# 判断某程序是否运行
- 多线程判断用户是否在线(后台运行ping脚本)
- 判断Mobile程序是否运行于真机之上
- winfrom判断程序是否运行,并给提示
- shell script里面判断某个程序是否还在运行
- VB程序怎么判断另一个程序是否正在运行?
- 判断程序是否运行在虚拟机中的代码,请人翻译