Pages

Android: Code for detecting if specific application or service running

For checking a application is running or not :
public static boolean isThisApplicationRunning(final Context context, final String appPackage) {
 if (appPackage == null) {
  return false;
 }
 final ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 final List<RunningAppProcessInfo> runningAppProcesses = manager.getRunningAppProcesses();
 for (final RunningAppProcessInfo app : runningAppProcesses) {
  if (appPackage.equals(app.processName)) {
   return true;
  }
 }
 return false;
}



For checking a service is running or not :
public static boolean isThisServiceRunning( final Context context, final String servicePackageName) {
 final ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 for (final RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  if (servicePackageName.equals(service.service.getClassName())) {
   return true;
  }
 }
 return false;
}



Required Permission :
 <uses-permission android:name="android.permission.GET_TASKS" />

No comments:

Post a Comment

Your Comment and Question will help to make this blog better...