Java Code Examples for android.app.activitymanager#RunningAppProcessInfo

The following examples show how to use android.app.activitymanager#RunningAppProcessInfo . 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: 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 2
Source File: DeviceInfo.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前进程name
 *
 * @param context
 * @return
 */
public static String getCurProcessName(Context context) {

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

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

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

        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}
 
Example 3
Source File: MyApp.java    From LQRWeChat with MIT License 6 votes vote down vote up
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 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: Utils.java    From KUtils-master 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 6
Source File: MemoryTracker.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    mAm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    // catch up in case we crashed but other processes are still running
    List<ActivityManager.RunningServiceInfo> svcs = mAm.getRunningServices(256);
    for (ActivityManager.RunningServiceInfo svc : svcs) {
        if (svc.service.getPackageName().equals(getPackageName())) {
            Log.v(TAG, "discovered running service: " + svc.process + " (" + svc.pid + ")");
            startTrackingProcess(svc.pid, svc.process,
                    System.currentTimeMillis() - (SystemClock.elapsedRealtime() - svc.activeSince));
        }
    }

    List<ActivityManager.RunningAppProcessInfo> procs = mAm.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo proc : procs) {
        final String pname = proc.processName;
        if (pname.startsWith(getPackageName())) {
            Log.v(TAG, "discovered other running process: " + pname + " (" + proc.pid + ")");
            startTrackingProcess(proc.pid, pname, System.currentTimeMillis());
        }
    }
}
 
Example 7
Source File: FragmentPreferences.java    From GcmForMojo with GNU General Public License v3.0 5 votes vote down vote up
private boolean shouldInit() {
    ActivityManager am = ((ActivityManager) getInstance().getSystemService(Context.ACTIVITY_SERVICE));
    List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
    String mainProcessName = getInstance().getPackageName();
    int myPid = android.os.Process.myPid();
    for (ActivityManager.RunningAppProcessInfo info : processInfos) {
        if (info.pid == myPid && mainProcessName.equals(info.processName)) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: HotfixLoaderUtil.java    From ZeusHotfix with MIT License 5 votes vote down vote up
/**
 * 获取当前进程的进程名
 *
 * @param context
 * @return
 */
private static String getCurProcessName(Context context) {
    int pid = Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}
 
Example 9
Source File: ProcessPhoenix.java    From ProcessPhoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the current process is a temporary Phoenix Process.
 * This can be used to avoid initialisation of unused resources or to prevent running code that
 * is not multi-process ready.
 *
 * @return true if the current process is a temporary Phoenix Process
 */
public static boolean isPhoenixProcess(Context context) {
  int currentPid = Process.myPid();
  ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
  if (runningProcesses != null) {
    for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
      if (processInfo.pid == currentPid && processInfo.processName.endsWith(":phoenix")) {
        return true;
      }
    }
  }
  return false;
}
 
Example 10
Source File: EnvInfo.java    From android-netdiag with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static boolean isBackground(Context context) {
    if (context == null) {
        return true;
    }
    String pkgName = context.getPackageName();
    if (pkgName == null) {
        return true;
    }
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();

    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (pkgName.equals(appProcess.processName)) {
            if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                Log.i(context.getPackageName(), "Background"
                        + appProcess.processName);
                return true;
            } else {
                Log.i(context.getPackageName(), "Foreground"
                        + appProcess.processName);
                return false;
            }
        }
    }

    return false;
}
 
Example 11
Source File: AlarmReceiver.java    From react-native-local-notifications with MIT License 5 votes vote down vote up
private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    final String packageName = context.getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: CondomCore.java    From MiPushFramework with GNU General Public License v3.0 5 votes vote down vote up
boolean isUidNotBackground(final int uid) {
	if (running_processes != null) {
		for (final ActivityManager.RunningAppProcessInfo running_process : running_processes)
			if (running_process.pid != 0 && running_process.importance < IMPORTANCE_BACKGROUND && running_process.uid == uid)
				return true;	// Same UID does not guarantee same process. This is spared intentionally.
	} else if (running_services != null) {
		for (final ActivityManager.RunningServiceInfo running_service : running_services)
			if (running_service.pid != 0 && running_service.uid == uid)	// Same UID does not guarantee same process. This is spared intentionally.
				return true;	// Only running process is qualified, although getRunningServices() may not include all running app processes.
	}
	return false;	// Fallback: Always treat as background app, since app with same UID will not reach here.
}
 
Example 13
Source File: CommonUtils.java    From SmartChart with Apache License 2.0 5 votes vote down vote up
/**
     * 判断应用前后台
     */

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.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("info", "此appimportace ="
                        + appProcess.importance
                        + ",context.getClass().getName()="
                        + context.getClass().getName());
                if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
//                    Log.i("info", "处于后台"
//                            + appProcess.processName);
                    return true;
                } else {
//                    Log.i("info", "处于前台"
//                            + appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }
 
Example 14
Source File: SystemUtil.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
public static String obtainProcessName(Context context) {
    final int pid = android.os.Process.myPid();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> listTaskInfo = am.getRunningAppProcesses();
    if (listTaskInfo != null && !listTaskInfo.isEmpty()) {
        for (ActivityManager.RunningAppProcessInfo info : listTaskInfo) {
            if (info != null && info.pid == pid) {
                return info.processName;
            }
        }
    }
    return null;
}
 
Example 15
Source File: CondomCore.java    From condom with Apache License 2.0 5 votes vote down vote up
boolean isUidNotBackground(final int uid) {
	if (running_processes != null) {
		for (final ActivityManager.RunningAppProcessInfo running_process : running_processes)
			if (running_process.pid != 0 && running_process.importance < IMPORTANCE_BACKGROUND && running_process.uid == uid)
				return true;	// Same UID does not guarantee same process. This is spared intentionally.
	} else if (running_services != null) {
		for (final ActivityManager.RunningServiceInfo running_service : running_services)
			if (running_service.pid != 0 && running_service.uid == uid)	// Same UID does not guarantee same process. This is spared intentionally.
				return true;	// Only running process is qualified, although getRunningServices() may not include all running app processes.
	}
	return false;	// Fallback: Always treat as background app, since app with same UID will not reach here.
}
 
Example 16
Source File: ForegroundServiceContext.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
private boolean isAppOnForeground() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    final String packageName = getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}
 
Example 17
Source File: CommonUtils.java    From TestChat with Apache License 2.0 5 votes vote down vote up
public static boolean isAppAlive(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcessInfo :
                appProcessInfos
                ) {
                if (appProcessInfo.processName.equals(context.getPackageName())) {
                        LogUtil.e("当前的APP存活");
                        return true;
                }
        }
        return false;
}
 
Example 18
Source File: AppUtil.java    From android-common with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前进程名字
 */
public static String getCurrentProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}
 
Example 19
Source File: AutoInputAction.java    From XposedSmsCode with GNU General Public License v3.0 4 votes vote down vote up
private boolean autoInputBlockedHere() {
    boolean result = false;
    try {
        List<String> blockedAppList = new ArrayList<>();
        try {
            Uri appInfoUri = DBProvider.APP_INFO_URI;
            ContentResolver resolver = mAppContext.getContentResolver();

            final String packageColumn = AppInfoDao.Properties.PackageName.columnName;
            final String blockedColumn = AppInfoDao.Properties.Blocked.columnName;

            String[] projection = {packageColumn,};
            String selection = blockedColumn + " = ?";
            String[] selectionArgs = {String.valueOf(1)};
            Cursor cursor = resolver.query(appInfoUri, projection, selection, selectionArgs, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    blockedAppList.add(cursor.getString(cursor.getColumnIndex(packageColumn)));
                }
                cursor.close();
            }
            XLog.d("Get blocked apps by content provider");
        } catch (Exception e) {
            List<AppInfo> appInfoList = EntityStoreManager
                    .loadEntitiesFromFile(EntityType.BLOCKED_APP, AppInfo.class);
            for (AppInfo appInfo : appInfoList) {
                blockedAppList.add(appInfo.getPackageName());
            }
            XLog.d("Get blocked apps from file");
        }

        if (blockedAppList.isEmpty()) {
            return false;
        }

        List<ActivityManager.RunningTaskInfo> runningTasks = getRunningTasks(mPhoneContext);
        String topPkgPrimary = null;
        if (runningTasks != null && runningTasks.size() > 0) {
            topPkgPrimary = runningTasks.get(0).topActivity.getPackageName();
            XLog.d("topPackagePrimary: %s", topPkgPrimary);
        }

        if (topPkgPrimary != null && blockedAppList.contains(topPkgPrimary)) {
            return true;
        }

        // RunningAppProcess 判断当前的进程不是很准确,所以用作次要参考
        List<ActivityManager.RunningAppProcessInfo> appProcesses = getRunningAppProcesses(mPhoneContext);
        if (appProcesses == null) {
            return false;
        }

        String[] topPkgSecondary = appProcesses.get(0).pkgList;
        String topProcessSecondary = appProcesses.get(0).processName;
        XLog.d("topProcessSecondary: %s, topPackages: %s", topProcessSecondary, Arrays.toString(topPkgSecondary));

        if (blockedAppList.contains(topProcessSecondary)) {
            result = true;
        } else {
            for (String topPackage : topPkgSecondary) {
                if (blockedAppList.contains(topPackage)) {
                    result = true;
                    break;
                }
            }
        }
    } catch (Throwable t) {
        XLog.e("", t);
    }
    return result;
}
 
Example 20
Source File: Util.java    From FloatingModMenu with GNU General Public License v3.0 4 votes vote down vote up
static boolean isAppBackground() {
    ActivityManager.RunningAppProcessInfo runningAppProcessInfo = new ActivityManager.RunningAppProcessInfo();
    ActivityManager.getMyMemoryState(runningAppProcessInfo);
    return runningAppProcessInfo.importance != 100;
}