code: public static String getPackageNameFromPid(Context context, int pid) {
          ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
          List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
          for (RunningAppProcessInfo info : processes) {
              if (info.pid == pid) {
                  String[] packages = info.pkgList;
                  if (packages.length > 0) {
                      return packages[0];
                  }
                  break;
              }
          }
          return null;
      }
docs: Get package name of the process id.
  @param context the context.
  @param pid the process id.
  @return the package name for the process id. {@code null} if no process found.