Example usage for android.content Context ACTIVITY_SERVICE

List of usage examples for android.content Context ACTIVITY_SERVICE

Introduction

In this page you can find the example usage for android.content Context ACTIVITY_SERVICE.

Prototype

String ACTIVITY_SERVICE

To view the source code for android.content Context ACTIVITY_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.ActivityManager for interacting with the global system state.

Usage

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;//  www. j  a  v a2 s  .  co  m
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }

    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null)
        for (RunningAppProcessInfo process : processList) {
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
    return count;
}

From source file:Main.java

public static String getProcessName(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> raps = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo rap : raps) {
        if (rap != null && rap.pid == pid) {
            return rap.processName;
        }/*  www . j  ava  2 s . c o m*/
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("WeakerAccess")
public static String getProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
    if (runningApps == null) {
        return null;
    }//from  www.j av  a  2s.co  m
    for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
        if (procInfo.pid == pid) {
            return procInfo.processName;
        }
    }
    return null;
}

From source file:Main.java

@Nullable
public static String getCurrentRunningActivityPackage(@NonNull Context context) {
    try {//from w  w w . ja  va2  s.  co m
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        return am.getRunningTasks(1).get(0).topActivity.getPackageName();
    } catch (Exception e) { /* sad, but true */
    }
    return null;
}

From source file:Main.java

public static boolean isServiceRunning(Context context, Class<?> serviceClass) {
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningServiceInfo> services = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
        if (runningServiceInfo.service.getClassName().equals(serviceClass.getName())) {
            return true;
        }/*w  ww  .  j a  v  a 2  s . com*/
    }
    return false;
}

From source file:Main.java

public static String getCurProcessName(Context context) {
    String strRet = null;/*  w ww  . j a  va 2  s.c  o m*/
    try {
        Class<?> clazz = Class.forName("android.ddm.DdmHandleAppName");
        Method method = clazz.getDeclaredMethod("getAppName");
        strRet = (String) method.invoke(clazz);
    } catch (Exception e) {
        Log.w(TAG, e);
    }
    if (TextUtils.isEmpty(strRet)) {
        final int pid = android.os.Process.myPid();
        android.app.ActivityManager activityManager = (android.app.ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        final List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : runningAppProcesses) {
            if (appProcess.pid == pid) {
                strRet = appProcess.processName;
                break;
            }
        }
    }
    return strRet;
}

From source file:Main.java

/**
 * Checks if the application is in the background (i.e behind another application's Activity).
 * // w w  w .  j a  v a2 s .  c o m
 * @param context
 * @return true if another application is above this one.
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

/**
 * Check if a service is running//from   ww  w.j  a v  a  2 s  .c o m
 * @param context
 * @param serviceClassName - complete name of service
 * @return
 */
public static boolean isServiceRunning(Context context, String serviceClassName) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClassName.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;//  ww w  .jav  a 2  s . c o m
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        os.writeBytes(Command + "\n");
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    Log.i("AppUtil", processName);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }

        activityManager.killBackgroundProcesses(packageName);

        //
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.deltadna.android.sdk.notifications.Utils.java

static boolean inForeground(Context context) {
    final List<RunningAppProcessInfo> processes = ((ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();

    if (processes != null) {
        for (final RunningAppProcessInfo process : processes) {
            if (process.processName.equals(context.getPackageName())
                    && process.importance == IMPORTANCE_FOREGROUND) {
                return true;
            }//  ww  w . ja va  2 s . c om
        }
    }

    return false;
}