예제 #1
0
  /**
   * 写文件
   *
   * @param 文件路径
   * @param 内容列表
   * @param 附加是附加的 ,如果这是真的,其他写的文件,明确内容的文件和写进去
   * @return 否则返回假如果contentList是空的,真的
   * @throws RuntimeException if an error occurs while operator FileWriter
   */
  public static boolean writeFile(String filePath, List<String> contentList, boolean append) {
    if (ListUtils.isEmpty(contentList)) {
      return false;
    }

    FileWriter fileWriter = null;
    try {
      makeDirs(filePath);
      fileWriter = new FileWriter(filePath, append);
      int i = 0;
      for (String line : contentList) {
        if (i++ > 0) {
          fileWriter.write("\r\n");
        }
        fileWriter.write(line);
      }
      fileWriter.close();
      return true;
    } catch (IOException e) {
      throw new RuntimeException("IOException occurred. ", e);
    } finally {
      if (fileWriter != null) {
        try {
          fileWriter.close();
        } catch (IOException e) {
          throw new RuntimeException("IOException occurred. ", e);
        }
      }
    }
  }
예제 #2
0
  /**
   * whether the app whost package's name is packageName is on the top of the stack
   *
   * <ul>
   *   <strong>Attentions:</strong>
   *   <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest
   * </ul>
   *
   * @param context
   * @param packageName
   * @return if params error or task stack is null, return null, otherwise retun whether the app is
   *     on the top of stack
   */
  public static Boolean isTopActivity(Context context, String packageName) {
    if (context == null || StringUtils.isEmpty(packageName)) {
      return null;
    }

    ActivityManager activityManager =
        (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
    if (ListUtils.isEmpty(tasksInfo)) {
      return null;
    }
    try {
      return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
예제 #3
0
  /**
   * 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>if {@link ActivityManager#getRunningAppProcesses()} is null, return false
   *       <li>if one process of {@link ActivityManager#getRunningAppProcesses()} is equal to
   *           processName, return true, otherwise return false
   *     </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;
  }