Java Code Examples for android.app.activitymanager#getRunningAppProcesses()

The following examples show how to use android.app.activitymanager#getRunningAppProcesses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IPluginManagerImpl.java    From DroidPlugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: CallNotificationManager.java    From react-native-twilio-programmable-voice with MIT License 6 votes vote down vote up
public int getApplicationImportance(ReactApplicationContext context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    if (activityManager == null) {
        return 0;
    }
    List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    if (processInfos == null) {
        return 0;
    }

    for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
        if (processInfo.processName.equals(context.getApplicationInfo().packageName)) {
            return processInfo.importance;
        }
    }
    return 0;
}
 
Example 3
Source File: AppUtil.java    From SimpleProject with MIT License 6 votes vote down vote up
/**
 * 获取APP的进程名
 * @param context
 * @return
 */
public static String getProcessName(Context context) {
	int pid = Process.myPid();
	ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	if (am == null) {
		return null;
	}

	for (ActivityManager.RunningAppProcessInfo process : am.getRunningAppProcesses()) {
		if (process.pid == pid) {
			return process.processName;
		}
	}

	return null;
}
 
Example 4
Source File: HKApplication.java    From Tpay with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取APP的进程名
 *
 * @param pID
 * @return
 */
private String getAppName(int pID) {
    String processName = null;
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = this.getPackageManager();
    while (i.hasNext()) {
        ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
        try {
            if (info.pid == pID) {
                processName = info.processName;
                return processName;
            }
        } catch (Exception e) {
            // Log.d("Process", "Error>> :"+ e.toString());
        }
    }
    return processName;
}
 
Example 5
Source File: SystemProcessor.java    From nono-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获得当前进程的名字
 *
 * @param context
 * @return 进程号
 */
public static String getCurProcessName(Context context) {

    int pid = android.os.Process.myPid();

    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
            .getRunningAppProcesses()) {

        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}
 
Example 6
Source File: CommonUtils.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the RunningAppProcessInfo object for the given package, or null if it cannot be found.
 */
public static ActivityManager.RunningAppProcessInfo getAppProcessInfo(
    String packageName, Context context) {
  final ActivityManager actman =
      (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  final List<ActivityManager.RunningAppProcessInfo> processes = actman.getRunningAppProcesses();
  ActivityManager.RunningAppProcessInfo procInfo = null;
  // According to docs, the result of getRunningAppProcesses can be null instead of empty.
  // Yay.
  if (processes != null) {
    for (ActivityManager.RunningAppProcessInfo info : processes) {
      if (info.processName.equals(packageName)) {
        procInfo = info;
        break;
      }
    }
  }
  return procInfo;
}
 
Example 7
Source File: HXSDKHelper.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * check the application process name if process name is not qualified, then we think it is a service process and we will not init SDK
 * @param pID
 * @return
 */
private String getAppName(int pID) {
    String processName = null;
    ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = appContext.getPackageManager();
    while (i.hasNext()) {
        ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
        try {
            if (info.pid == pID) {
                CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                // Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
                // info.processName +"  Label: "+c.toString());
                // processName = c.toString();
                processName = info.processName;
                return processName;
            }
        } catch (Exception e) {
            // Log.d("Process", "Error>> :"+ e.toString());
        }
    }
    return processName;
}
 
Example 8
Source File: Utils.java    From KUtils with Apache License 2.0 6 votes vote down vote up
/**
 * whether this process is named with processName
 *
 * @param context     上下文
 * @param processName 进程名
 * @return 是否含有当前的进程
 */
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<ActivityManager.RunningAppProcessInfo> processInfoList
            = manager.getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid &&
                processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: ProtectorUtils.java    From StartUp-Protector with Apache License 2.0 6 votes vote down vote up
/**
 * is main process
 *
 * @param context
 * @return
 */
public static boolean isMainProcess(Context context) {
    try {
        ActivityManager am = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfo = am.getRunningAppProcesses();
        String mainProcessName = context.getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfo) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 10
Source File: BackgroundUtil.java    From AndroidProcess with Apache License 2.0 5 votes vote down vote up
/**
 * 方法2:通过getRunningAppProcesses的IMPORTANCE_FOREGROUND属性判断是否位于前台,当service需要常驻后台时候,此方法失效,
 * 在小米 Note上此方法无效,在Nexus上正常
 *
 * @param context     上下文参数
 * @param packageName 需要检查是否位于栈顶的App的包名
 * @return
 */
public static boolean getRunningAppProcesses(Context context, String packageName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: Utils.java    From DroidPlugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getProcessName(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> raps = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo rap : raps) {
        if (rap != null && rap.pid == pid) {
            return rap.processName;
        }
    }
    return null;
}
 
Example 12
Source File: AliPlugin.java    From Hook with Apache License 2.0 5 votes vote down vote up
public String getProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid) {
            return processInfo.processName;
        }
    }
    return null;
}
 
Example 13
Source File: CommonUtils.java    From Android_framework with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 获取进程名
 */
public static String getCurrentProcessName(Context context){
    String currentProcessName = "";
    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
    {
        if (processInfo.pid == pid)
        {
            currentProcessName = processInfo.processName;
            break;
        }
    }
    return currentProcessName;
}
 
Example 14
Source File: AppInfoUtil.java    From BaseProject with MIT License 5 votes vote down vote up
/**
 * 获取当前运行的所有进程名
 */
public static List<String> getProcessName(@NonNull Context context, @NonNull String packageName) {
    List<String> list = new ArrayList<>();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (null == activityManager) {
        return null;
    }
    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
        if (appProcess.processName.startsWith(packageName)) {
            list.add(appProcess.processName);
        }
    }
    return list;
}
 
Example 15
Source File: FileDownloadUtils.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
private static boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager == null) return false;

    List<ActivityManager.RunningAppProcessInfo> appProcesses =
            activityManager.getRunningAppProcesses();
    if (appProcesses == null) return false;

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm == null) return false;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        if (!pm.isInteractive()) return false;
    } else {
        if (!pm.isScreenOn()) return false;
    }

    String packageName = context.getApplicationContext().getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName) && appProcess.importance
                == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }

    }
    return false;
}
 
Example 16
Source File: AppUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 判断App是否处于前台
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isAppForeground() {
    ActivityManager manager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> info = manager.getRunningAppProcesses();
    if (info == null || info.size() == 0) return false;
    for (ActivityManager.RunningAppProcessInfo aInfo : info) {
        if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return aInfo.processName.equals(Utils.getContext().getPackageName());
        }
    }
    return false;
}
 
Example 17
Source File: HookHelper.java    From ConfessTalkKiller with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 18
Source File: SystemUtils.java    From RunMap with Apache License 2.0 5 votes vote down vote up
public static String getProcessName(Context context) {
    int pid = android.os.Process.myPid();//获取进程pid
    String processName = "";
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);//获取系统的ActivityManager服务
    for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            processName = appProcess.processName;
            return processName;
        }
    }
    return processName;
}
 
Example 19
Source File: MyApplication.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
/**
 * is main process
 */
private static boolean isMainProcess(Application application) {
    int pid = android.os.Process.myPid();
    String processName = "";
    ActivityManager manager = (ActivityManager) application.getSystemService
            (Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
        if (process.pid == pid) {
            processName = process.processName;
        }
    }
    return application.getPackageName().equals(processName);
}
 
Example 20
Source File: BootstrapApplication.java    From android-advanced-decode with MIT License 4 votes vote down vote up
public void onCreate() {
	if (!AppInfo.usingApkSplits) {
		MonkeyPatcher.monkeyPatchApplication(this, this,
				this.realApplication, this.externalResourcePath);

		MonkeyPatcher.monkeyPatchExistingResources(this,
				this.externalResourcePath, null);
	} else {
		MonkeyPatcher.monkeyPatchApplication(this, this,
				this.realApplication, null);
	}
	super.onCreate();
	if (AppInfo.applicationId != null) {
		try {
			boolean foundPackage = false;
			int pid = Process.myPid();
			ActivityManager manager = (ActivityManager) getSystemService("activity");

			List<ActivityManager.RunningAppProcessInfo> processes = manager
					.getRunningAppProcesses();
			boolean startServer = false;
			if ((processes != null) && (processes.size() > 1)) {
				for (ActivityManager.RunningAppProcessInfo processInfo : processes) {
					if (AppInfo.applicationId
							.equals(processInfo.processName)) {
						foundPackage = true;
						if (processInfo.pid == pid) {
							startServer = true;
							break;
						}
					}
				}
				if ((!startServer) && (!foundPackage)) {
					startServer = true;
					if (Log.isLoggable("InstantRun", 2)) {
						Log.v("InstantRun",
								"Multiprocess but didn't find process with package: starting server anyway");
					}
				}
			} else {
				startServer = true;
			}
			if (startServer) {
				Server.create(AppInfo.applicationId, this);
			}
		} catch (Throwable t) {
			if (Log.isLoggable("InstantRun", 2)) {
				Log.v("InstantRun", "Failed during multi process check", t);
			}
			Server.create(AppInfo.applicationId, this);
		}
	}
	if (this.realApplication != null) {
		this.realApplication.onCreate();
	}
}