Skip to content

Instantly share code, notes, and snippets.

@shawnfeng0
Created November 8, 2018 18:13
Show Gist options
  • Save shawnfeng0/445aee6505cf1a39353eea333e851fa5 to your computer and use it in GitHub Desktop.
Save shawnfeng0/445aee6505cf1a39353eea333e851fa5 to your computer and use it in GitHub Desktop.
/**
* Is the Android service running?
* @param context The current context.
* @param serviceClass The service to get running state.
* @return true: if service is running.
*/
static boolean isServiceRunning(Context context, Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (manager == null) return false;
for (ActivityManager.RunningServiceInfo serviceInfo : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceInfo.service.getClassName().equals(serviceClass.getName())) {
return true;
}
}
return false;
}
/**
* Find integer string from a string.
* @param str The string to get integer string.
* @return The resulting integer string.
*/
static List<String> findIntegerString(String str) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
result.add(str.substring(matcher.start(),matcher.end()));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment