List of usage examples for android.app ActivityManager getRunningAppProcesses
public List<RunningAppProcessInfo> getRunningAppProcesses()
From source file:Main.java
public static boolean isAppOnForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); // Returns a list of application processes that are running on the // device//from w w w. j a va 2s. c om List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName()) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }
From source file:Main.java
public static String getCurProcessName(Context context) { int pid = android.os.Process.myPid(); ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (activityManager == null) { return null; }//from w w w. j a v a 2 s .c o m List<RunningAppProcessInfo> runningProcessList = activityManager.getRunningAppProcesses(); if (runningProcessList == null || runningProcessList.isEmpty()) { return null; } for (ActivityManager.RunningAppProcessInfo appProcess : runningProcessList) { if (appProcess.pid == pid) { return appProcess.processName; } } return null; }
From source file:Main.java
/** * Method checks if the app is in background or not *///from w w w . j a v a 2s . co m public static boolean isAppIsInBackground(Context context) { boolean isInBackground = true; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (String activeProcess : processInfo.pkgList) { if (activeProcess.equals(context.getPackageName())) { isInBackground = false; } } } } } else { List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; if (componentInfo.getPackageName().equals(context.getPackageName())) { isInBackground = false; } } return isInBackground; }
From source file:Main.java
public static String getCurProcessName(Context context) { String strRet = null;//from www . java 2 s. c om try { Class<?> clazz = Class.forName("android.ddm.DdmHandleAppName"); Method method = clazz.getDeclaredMethod("getAppName"); strRet = (String) method.invoke(clazz); } catch (Exception e) { Log.w(TAG, e); } if (TextUtils.isEmpty(strRet)) { final int pid = android.os.Process.myPid(); android.app.ActivityManager activityManager = (android.app.ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); final List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : runningAppProcesses) { if (appProcess.pid == pid) { strRet = appProcess.processName; break; } } } return strRet; }
From source file:Main.java
public static boolean isAppOnForeground(Context ctx) { ActivityManager activityManager = (ActivityManager) ctx.getApplicationContext()// .getSystemService(Context.ACTIVITY_SERVICE); String packageName = ctx.getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; }//from w ww .jav a 2s.c om for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }
From source file:Main.java
public static boolean isAppOnBackground(Context context) { // Returns a list of application processes that are running on the // device//from w w w.j a v a 2s.c o m ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = context.getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) { return true; } } return false; }
From source file:Main.java
public static boolean isAppOnForeground(Context context) { // Returns a list of application processes that are running on the // device//www. j a v a2s . c o m ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = context.getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }
From source file:com.tencent.mars.sample.SampleApplicaton.java
public static void openXlog() { if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (BuildConfig.DEBUG) { Xlog.setConsoleLogOpen(true); } else {/*from ww w . j ava2 s . co m*/ Xlog.setConsoleLogOpen(false); } Log.setLogImp(new Xlog()); } else { int pid = android.os.Process.myPid(); String processName = null; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) { if (appProcess.pid == pid) { processName = appProcess.processName; break; } } final String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath(); final String logPath = SDCARD + "/marssample/log"; String logFileName = processName.indexOf(":") == -1 ? "MarsSample" : ("MarsSample_" + processName.substring(processName.indexOf(":") + 1)); if (BuildConfig.DEBUG) { Xlog.appenderOpen(Xlog.LEVEL_VERBOSE, Xlog.AppednerModeAsync, "", logPath, logFileName); Xlog.setConsoleLogOpen(true); } else { Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, "", logPath, logFileName); Xlog.setConsoleLogOpen(false); } Log.setLogImp(new Xlog()); } }
From source file:nu.yona.app.api.service.ActivityMonitorService.java
private static String printForegroundTask(Context context) { currentApp = "NULL"; try {/* ww w . ja v a 2 s . c om*/ if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); long time = System.currentTimeMillis(); List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - AppConstant.ONE_SECOND * AppConstant.ONE_SECOND, time); if (appList != null && appList.size() > 0) { SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); for (UsageStats usageStats : appList) { mySortedMap.put(usageStats.getLastTimeUsed(), usageStats); } if (!mySortedMap.isEmpty()) { currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); } } } else { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); currentApp = am.getRunningAppProcesses().get(0).processName; } } catch (Exception e) { AppUtils.reportException(ActivityMonitorService.class.getSimpleName(), e, Thread.currentThread()); } return currentApp; }
From source file:com.gaba.alex.trafficincidents.Utility.java
private static boolean isAppOnForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses != null) { final String packageName = context.getPackageName(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; }/*from w w w . j av a 2 s .c o m*/ } } return false; }