@SuppressLint({"NewApi"}) public static boolean isAppOnForeground(Context mContext) { ActivityManager activityManager = (ActivityManager) mContext.getSystemService("activity"); String packageName = mContext.getPackageName(); LogInfo.log("PushReceiver", "packageName =" + packageName); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { LogInfo.log("PushReceiver", "------appProcesses == null-----"); return false; } for (RunningAppProcessInfo appProcess : appProcesses) { LogInfo.log("PushReceiver", "------appProcess.processName =" + appProcess.processName); if (appProcess.processName.equals(packageName) && appProcess.importance == 100) { for (RunningTaskInfo rti : activityManager.getRunningTasks(100)) { if (!(rti == null || rti.baseActivity == null || mContext.getPackageName() == null || !mContext.getPackageName().equals(rti.baseActivity.getPackageName()) || VERSION.SDK_INT < 11)) { activityManager.moveTaskToFront(rti.id, 1); } } return true; } } return false; }
/** * whether this process is named with processName * * @param context * @param processName * @return <ul> * return whether this process is named with processName * <li>if context is null, return false</li> * <li>if {@link ActivityManager#getRunningAppProcesses()} is null, return false</li> * <li>if one process of {@link ActivityManager#getRunningAppProcesses()} is equal to processName, return * true, otherwise return false</li> * </ul> */ public static boolean isNamedProcess(Context context, String processName) { if (context == null) { return false; } int pid = android.os.Process.myPid(); ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses(); if (ListUtils.isEmpty(processInfoList)) { return false; } for (RunningAppProcessInfo processInfo : processInfoList) { if (processInfo != null && processInfo.pid == pid && ObjectUtils.isEquals(processName, processInfo.processName)) { return true; } } return false; }
public static String getMyProcessName(Context context) { String processName = ""; ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses(); if (infoList == null) { throw new RuntimeException("getRunningAppProcesses() returns 'null'."); } for (RunningAppProcessInfo info : infoList) { try { if (info.pid == Process.myPid()) { processName = info.processName; break; } } catch (Exception ignored) { } } return processName; }
/** * Asks if the current process is "org.fdroid.fdroid:acra". * <p> * This is helpful for bailing out of the {@link FDroidApp#onCreate} method early, preventing * problems that arise from executing the code twice. This happens due to the `android:process` * statement in AndroidManifest.xml causes another process to be created to run * {@link org.fdroid.fdroid.acra.CrashReportActivity}. This was causing lots of things to be * started/run twice including {@link CleanCacheService} and {@link WifiStateChangeService}. * <p> * Note that it is not perfect, because some devices seem to not provide a list of running app * processes when asked. In such situations, F-Droid may regress to the behaviour where some * services may run twice and thus cause weirdness or slowness. However that is probably better * for end users than experiencing a deterministic crash every time F-Droid is started. */ private boolean isAcraProcess() { ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses(); if (processes == null) { return false; } int pid = android.os.Process.myPid(); for (RunningAppProcessInfo processInfo : processes) { if (processInfo.pid == pid && ACRA_ID.equals(processInfo.processName)) { return true; } } return false; }
private boolean isAppOnForeground() { ActivityManager aManager=((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)); List<RunningAppProcessInfo> appProcesses = aManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(getPackageName())){ Log.e("StopListennerActivity", "appProcess.importance="+appProcess.importance); // inRunningTasks(aManager); if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; }else{ return false; } } } return false; }
private static String getProcessName(Context context) { try { String currentProcessName = null; int pid = android.os.Process.myPid(); ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { if (processInfo.pid == pid) { currentProcessName = processInfo.processName; break; } } return currentProcessName; } catch (SecurityException ex) { return null; } }
public List<App> getCurrentlyRunningMonitoredApps() { List<App> runningMonitoredApps = new ArrayList(); List<RunningAppProcessInfo> procInfos = ((ActivityManager) this.context.getSystemService("activity")).getRunningAppProcesses(); if (procInfos != null) { for (RunningAppProcessInfo procInfo : procInfos) { String processName = procInfo.processName; if (!shouldExcludeRunningApp(procInfo)) { for (App app : this.appsToMonitor) { if (app.getAppPackageName() != null && app.getAppPackageName().equals(processName)) { runningMonitoredApps.add(app); } } } } } return runningMonitoredApps; }
boolean shouldExcludeRunningApp(RunningAppProcessInfo procInfo) { try { int flags = RunningAppProcessInfo.class.getField("flags").getInt(procInfo); if (procInfo.importance != 100) { return true; } if ((flags & 2) == 2) { return true; } if ((flags & 4) == 0) { return true; } return false; } catch (NoSuchFieldException e) { return true; } catch (IllegalAccessException e2) { return true; } }
/** * whether this process is named with processName * * @param context 上下文 * @param processName 进程名 * @return <ul> * return whether this process is named with processName * <li>if context is null, return false</li> * <li>if {@link ActivityManager#getRunningAppProcesses()} is null, * return false</li> * <li>if one process of * {@link ActivityManager#getRunningAppProcesses()} is equal to * processName, return true, otherwise return false</li> * </ul> */ public static boolean isNamedProcess(Context context, String processName) { if (context == null || TextUtils.isEmpty(processName)) { return false; } int pid = android.os.Process.myPid(); ActivityManager manager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> processInfoList = manager .getRunningAppProcesses(); if (processInfoList == null) { return true; } for (RunningAppProcessInfo processInfo : manager .getRunningAppProcesses()) { if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) { return true; } } return false; }
public static boolean checkIsMainProcess(Application app) { ActivityManager activityManager = (ActivityManager) app.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningAppProcessInfoList = activityManager.getRunningAppProcesses(); if (null == runningAppProcessInfoList) { return false; } String currProcessName = null; int currPid = android.os.Process.myPid(); //find the process name for (RunningAppProcessInfo processInfo : runningAppProcessInfoList) { if (null != processInfo && processInfo.pid == currPid) { currProcessName = processInfo.processName; } } //is current process the main process if (!TextUtils.equals(currProcessName, app.getPackageName())) { return false; } return true; }
private static final void waitKilled(int pid) { for (int i = 0; i < 10; i++) { try { Thread.sleep(100, 0); } catch (Throwable e) { // } // List<RunningAppProcessInfo> processes = AMSUtils.getRunningAppProcessesNoThrows(RePluginInternal.getAppContext()); if (processes == null || processes.isEmpty()) { continue; } boolean found = false; for (RunningAppProcessInfo info : processes) { if (info.pid == pid) { found = true; } } if (!found) { return; } } }
@Override public boolean killBackgroundProcesses(String pluginPackageName) throws RemoteException { ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> infos = am.getRunningAppProcesses(); boolean success = false; for (RunningAppProcessInfo info : infos) { if (info.pkgList != null) { String[] pkgListCopy = Arrays.copyOf(info.pkgList, info.pkgList.length); Arrays.sort(pkgListCopy); if (Arrays.binarySearch(pkgListCopy, pluginPackageName) >= 0 && info.pid != android.os.Process.myPid()) { Log.i(TAG, "killBackgroundProcesses(%s),pkgList=%s,pid=%s", pluginPackageName, Arrays.toString(info.pkgList), info.pid); android.os.Process.killProcess(info.pid); success = true; } } } return success; }
public static String getCurrentProcessName(Context context) { if (context == null) return sCurrentProcessName; synchronized (sGetCurrentProcessNameLock) { if (sCurrentProcessName == null) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> infos = activityManager.getRunningAppProcesses(); if (infos == null) return null; for (RunningAppProcessInfo info : infos) { if (info.pid == android.os.Process.myPid()) { sCurrentProcessName = info.processName; return sCurrentProcessName; } } } } return sCurrentProcessName; }
private void doGc(RunningAppProcessInfo myInfo) { int activityCount = mRunningProcessList.getActivityCountByPid(myInfo.pid); int serviceCount = mRunningProcessList.getServiceCountByPid(myInfo.pid); int providerCount = mRunningProcessList.getProviderCountByPid(myInfo.pid); if (activityCount <= 0 && serviceCount <= 0 && providerCount <= 0) { //杀死空进程。 Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s)", myInfo.pid, myInfo.uid, myInfo.processName); try { android.os.Process.killProcess(myInfo.pid); } catch (Throwable e) { Log.e(TAG, "error on killProcess", e); } } else if (activityCount <= 0 && serviceCount > 0 /*&& !mRunningProcessList.isPersistentApplication(myInfo.pid)*/) { List<String> names = mRunningProcessList.getStubServiceByPid(myInfo.pid); if (names != null && names.size() > 0) { for (String name : names) { Intent service = new Intent(); service.setClassName(mHostContext.getPackageName(), name); AbstractServiceStub.startKillService(mHostContext, service); Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s) service=%s", myInfo.pid, myInfo.uid, myInfo.processName, service); } } } }
public boolean isMainProcess(Context context) { if (context == null) { return false; } Object obj; int myPid = Process.myPid(); for (RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context .getSystemService(ModelName.ACTIVITY)).getRunningAppProcesses()) { if (runningAppProcessInfo.pid == myPid) { obj = runningAppProcessInfo.processName; break; } } obj = null; return context.getPackageName().equals(obj); }
public static boolean isTinkerPatchServiceRunning(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(ModelName.ACTIVITY); String serverName = getTinkerPatchServiceName(context); if (serverName == null) { return false; } try { for (RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) { if (appProcess.processName.equals(serverName)) { return true; } } return false; } catch (Exception e) { Log.e(TAG, "isTinkerPatchServiceRunning Exception: " + e.toString()); return false; } catch (Error e2) { Log.e(TAG, "isTinkerPatchServiceRunning Error: " + e2.toString()); return false; } }
static String I(Context context) { try { if (aR != null) { return aR; } int myPid = Process.myPid(); for (RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context .getSystemService(ModelName.ACTIVITY)).getRunningAppProcesses()) { if (runningAppProcessInfo.pid == myPid) { aR = runningAppProcessInfo.processName; break; } } return aR; } catch (Throwable th) { } }
private void discoverStartedActivityPid() { ActivityManager activityManager = (ActivityManager) getContext() .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); List<RunningAppProcessInfo> apps = activityManager .getRunningAppProcesses(); String packageName = services.get(0).topActivity.getPackageName(); // fist // Task // is // the // last // started // task for (int i = 0; i < apps.size(); i++) { if (apps.get(i).processName.equals(packageName)) { appPid = apps.get(i).pid; Log.d(getClass().getName(), "Found activity process: " + apps.get(i).processName + " PID: " + appPid); } } }
private void discoverMusicActivityPid() { ActivityManager activityManager = (ActivityManager) getContext() .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); List<RunningAppProcessInfo> apps = activityManager.getRunningAppProcesses(); String packageName = services.get(0).topActivity.getPackageName(); //fist Task is the last started task for (int i = 0; i < apps.size(); i++) { if(apps.get(i).processName .equals(packageName)){ musicAppPid = apps.get(i).pid; Log.d(getClass().getName(), "Found music activity process: " + apps.get(i).processName + " PID: " + musicAppPid); } } }
private void doGc(RunningAppProcessInfo myInfo) { int activityCount = mRunningProcessList.getActivityCountByPid(myInfo.pid); int serviceCount = mRunningProcessList.getServiceCountByPid(myInfo.pid); int providerCount = mRunningProcessList.getProviderCountByPid(myInfo.pid); if (activityCount <= 0 && serviceCount <= 0 && providerCount <= 0) { //杀死空进程。 Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s)", myInfo.pid, myInfo.uid, myInfo.processName); android.os.Process.killProcess(myInfo.pid); } else if (activityCount <= 0 && serviceCount > 0 /*&& !mRunningProcessList.isPersistentApplication(myInfo.pid)*/) { List<String> names = mRunningProcessList.getStubServiceByPid(myInfo.pid); if (names != null && names.size() > 0) { for (String name : names) { Intent service = new Intent(); service.setClassName(mHostContext.getPackageName(), name); AbstractServiceStub.startKillService(mHostContext, service); Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s) service=%s", myInfo.pid, myInfo.uid, myInfo.processName, service); } } } }
/** * 根据包名判断应用在前台还是后台 * * @param context * @return */ public static boolean isBackground(Context context, String packageName) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName)) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { Log.i("前台", appProcess.processName); return false; } else { Log.i("后台", appProcess.processName); return true; } } } return true; }
/** * 判断应用是否在前台显示 * * @param context 上下文 * @param packageName 包名 * @return 返回true表示在前台显示 */ public static boolean isAppOnForeground(Context context, String packageName) { if (packageName == null || context == null) { return false; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 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; }
/** * 判断某个应用是否正在运行 * * @param context 上下文 * @param packageName 包名 * @return 正在运行返回true, 否则返回false */ public static boolean isAppOnRunning(Context context, String packageName) { if (packageName == null || context == null) { return false; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName)) { return true; } } return false; }
/** * ɱ�����н��� * @param ctx �����Ļ��� */ public static void killAll(Context ctx) { //1,��ȡactivityManager ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); //2,��ȡ�������н��̵ļ��� List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); //3,ѭ���������еĽ���,����ɱ�� for (RunningAppProcessInfo info : runningAppProcesses) { //4,�����ֻ���ʿ����,�����Ľ��̶���Ҫȥɱ�� if(info.processName.equals(ctx.getPackageName())){ //���ƥ�������ֻ���ʿ,����Ҫ��������ѭ��,������һ��Ѱ,����ɱ������ continue; } am.killBackgroundProcesses(info.processName); } }
/** * 程序是否在前台运行 * * @return */ public static boolean isAppOnForeground(Context context) { try { ActivityManager activityManager = (ActivityManager) context .getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE); String packageName = context.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; } } } catch (Exception e) { e.printStackTrace(); LogS.e(TAG, "isAppOnForeground exception!", e); } return false; }
private double getRunningAppProcessInfo() { double memSize; if (mRunningAppProcessInfo != null) { return getMemSize(mRunningAppProcessInfo) / 1024; } // 通过调用 ActivityManager 的 getRunningAppProcesses() 方法获得系统里所有正在运行的进程 List<RunningAppProcessInfo> appProcessList = mActivityManager .getRunningAppProcesses(); for (RunningAppProcessInfo appProcessInfo : appProcessList) { // 进程名,默认是包名或者由属性 android:process="" 指定 String processName = appProcessInfo.processName; memSize = getMemSize(appProcessInfo); if (processName.equals(mPackageName)) { mRunningAppProcessInfo = appProcessInfo; return memSize / 1024; } } return -1; }
private boolean applicationIsRunning(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses(); for (RunningAppProcessInfo processInfo : processInfos) { if (processInfo.processName.equals(context.getApplicationContext().getPackageName())) { if (processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (String d: processInfo.pkgList) { Log.v("ReactSystemNotification", "NotificationEventReceiver: ok: " + d); return true; } } } } } else { List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; if (componentInfo.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
/** * 给定Context获取进程名 * * @param context * @return */ public static String getProcessName(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses(); if (runningApps == null) { return null; } for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) { if (proInfo.pid == android.os.Process.myPid()) { if (proInfo.processName != null) { return proInfo.processName; } } } return null; }
@Override protected void getData() { List<RunningServiceInfo> liServices = m_activityManager.getRunningServices(MAXIMUM_SERVICES); List<RunningAppProcessInfo> runningAppProcesses = m_activityManager.getRunningAppProcesses(); // liTasks.get(0). // Account[] accounts = m_activityManager.getAccounts(); // String[] strAccountTypes = new String[accounts.length]; // // for (int i = 0; i < accounts.length; i++) { // strAccountTypes[i] = accounts[i].type; // } if (!liServices.isEmpty()) { // TODO: implement logic StringBuilder sb = new StringBuilder(); // for (int i = 0; i < liProcesses.size() - 1; i++) // sb.append(liProcesses.get.type + ";"); // sb.append(liProcesses[liProcesses.length - 1]); // SensorRunningServices sensor = new SensorRunningServices(); // // sensor.setAccountTypes(sb.toString()); // handleDBEntry(sensor); } }
public boolean isAppOnForeground() { // Returns a list of application processes that are running on the // device String packageName = context.getPackageName(); ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { // importance: // The relative importance level that the system places // on this process. // May be one of IMPORTANCE_FOREGROUND, IMPORTANCE_VISIBLE, // IMPORTANCE_SERVICE, IMPORTANCE_BACKGROUND, or IMPORTANCE_EMPTY. // These constants are numbered so that "more important" values are // always smaller than "less important" values. // processName: // 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; }
public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { /* * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200 */ Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance + ",context.getClass().getName()=" + context.getClass().getName()); if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { Log.i(context.getPackageName(), "处于后台" + appProcess.processName); return true; } else { Log.i(context.getPackageName(), "处于前台" + appProcess.processName); return false; } } } return false; }
public static void cleanMemory(Context context){ System.out.println("---> 清理前可用内存:"+getAvailMemory(context)+"MB"); ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses(); if (processList != null) { for (int i = 0; i < processList.size(); ++i) { RunningAppProcessInfo runningAppProcessInfo= processList.get(i); // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE // 的进程即为长时间未使用进程或者空进程 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE // 的进程都是非可见进程,即在后台运行 if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] pkgList = runningAppProcessInfo.pkgList; for (int j = 0; j < pkgList.length; ++j) { System.out.println("===> 正在清理:"+pkgList[j]); activityManager.killBackgroundProcesses(pkgList[j]); } } } } System.out.println("---> 清理后可用内存:"+getAvailMemory(context)+"MB"); }