ActivityManager判断app运行状态 发表于 2017-02-28 | 摘要: 在后台与网络交互中,需要使用Service来和服务器保持连接,因此Service在被进程Kill掉后,主动开启Service是必要的,因此首先的检测Service是否已启动 检测Service是否已启动1234567891011public static boolean isServiceRunning(String serviceClassName){ final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE); final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); for (RunningServiceInfo runningServiceInfo : services) { if (runningServiceInfo.service.getClassName().equals(serviceClassName)){ return true; } } return false; } 检测Activity是否已启动12<!-- 权限--><uses-permission android:name="android.permission.GET_TASKS"/> 1234567891011ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);List<RunningTaskInfo> list = am.getRunningTasks(100);boolean isAppRunning = false;String MY_PKG_NAME = "com.cyberblue.iitag";for (RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) { isAppRunning = true; Log.i(TAG,info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName()); break; }} 1234567891011121314151617private boolean isLauncherRunnig(Context context) { boolean result = false ; List<String> names = getAllTheLauncher(); ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ; List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ; for (RunningAppProcessInfo running : appList) { if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (int i = 0; i < names.size(); i++) { if (names.get(i).equals(running.processName)) { result = true ; break; } } } } return result ;} 判断App是否运行1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 /** * 判断app是否正在运行 * @param ctx * @param packageName * @return */public boolean appIsRunning(Context ctx,String packageName){ ActivityManager am = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); if(runningAppProcesses!=null) { for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if(runningAppProcessInfo.processName.startsWith(packageName)) { return true; } } } return false;}/** * app 是否在后台运行 * @param ctx * @param packageName * @return */public boolean appIsBackgroundRunning(Context ctx,String packageName){ ActivityManager am = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); if(runningAppProcesses!=null) { for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if(runningAppProcessInfo.processName.startsWith(packageName)) { return runningAppProcessInfo.importance!=RunningAppProcessInfo.IMPORTANCE_FOREGROUND && runningAppProcessInfo.importance!=RunningAppProcessInfo.IMPORTANCE_VISIBLE; //排除无界面的app } } } return false;} 赏 微信打赏 支付宝打赏