Search in sources :

Example 11 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project LshUtils by SenhLinsh.

the class AppUtils method gc.

/**
     * 清理后台进程与服务
     *
     * @return 被清理的数量
     */
public static int gc(Context context) {
    long i = getDeviceUsableMemory(context);
    // 清理掉的进程数
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // 获取正在运行的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++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }
    // 获取正在运行的进程列表
    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (RunningAppProcessInfo process : processList) {
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到该进程下运行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    if (DEBUG) {
                    }
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    if (DEBUG) {
    }
    return count;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 12 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project DroidPlugin by DroidPluginTeam.

the class PluginProcessManager method getCurrentProcessName.

public static String getCurrentProcessName(Context context) {
    if (context == null)
        return sCurrentProcessName;
    synchronized (sGetCurrentProcessNameLock) {
        if (sCurrentProcessName == null) {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> infos = activityManager.getRunningAppProcesses();
            if (infos == null)
                return null;
            for (RunningAppProcessInfo info : infos) {
                if (info.pid == android.os.Process.myPid()) {
                    sCurrentProcessName = info.processName;
                    return sCurrentProcessName;
                }
            }
        }
    }
    return sCurrentProcessName;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 13 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project DroidPlugin by DroidPluginTeam.

the class IPluginManagerImpl method killBackgroundProcesses.

@Override
public boolean killBackgroundProcesses(String pluginPackageName) throws RemoteException {
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
    boolean success = false;
    for (RunningAppProcessInfo info : infos) {
        if (info.pkgList != null) {
            String[] pkgListCopy = Arrays.copyOf(info.pkgList, info.pkgList.length);
            Arrays.sort(pkgListCopy);
            if (Arrays.binarySearch(pkgListCopy, pluginPackageName) >= 0 && info.pid != android.os.Process.myPid()) {
                Log.i(TAG, "killBackgroundProcesses(%s),pkgList=%s,pid=%s", pluginPackageName, Arrays.toString(info.pkgList), info.pid);
                android.os.Process.killProcess(info.pid);
                success = true;
            }
        }
    }
    return success;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 14 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project android_frameworks_base by DirtyUnicorns.

the class FingerprintService method isForegroundActivity.

private boolean isForegroundActivity(int uid, int pid) {
    try {
        List<RunningAppProcessInfo> procs = ActivityManagerNative.getDefault().getRunningAppProcesses();
        int N = procs.size();
        for (int i = 0; i < N; i++) {
            RunningAppProcessInfo proc = procs.get(i);
            if (proc.pid == pid && proc.uid == uid && proc.importance == IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
    } catch (RemoteException e) {
        Slog.w(TAG, "am.getRunningAppProcesses() failed");
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RemoteException(android.os.RemoteException) Fingerprint(android.hardware.fingerprint.Fingerprint)

Example 15 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method getErrorReportFromErrorLog.

@Test
public void getErrorReportFromErrorLog() throws DeviceInfoHelper.DeviceInfoException {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(SystemClock.elapsedRealtime()).thenReturn(1000L);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    Device mockDevice = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
    /* Mock process name. */
    ActivityManager activityManager = mock(ActivityManager.class);
    RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
    runningAppProcessInfo.pid = 123;
    runningAppProcessInfo.processName = "right.process";
    when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
    when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
    /* Mock architecture. */
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    Whitebox.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Create an error log. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new TestCrashException()), java.lang.Thread.getAllStackTraces(), 900, true);
    assertNotNull(errorLog);
    /* Test. */
    Throwable throwable = new RuntimeException();
    ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    assertNotNull(report);
    assertEquals(errorLog.getId().toString(), report.getId());
    assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
    assertEquals(throwable, report.getThrowable());
    assertEquals(errorLog.getToffset() - errorLog.getAppLaunchTOffset(), report.getAppStartTime().getTime());
    assertEquals(errorLog.getToffset(), report.getAppErrorTime().getTime());
    assertEquals(errorLog.getDevice(), report.getDevice());
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Device(com.microsoft.azure.mobile.ingestion.models.Device) Build(android.os.Build) ActivityManager(android.app.ActivityManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)35 ActivityManager (android.app.ActivityManager)27 IActivityManager (android.app.IActivityManager)6 RemoteException (android.os.RemoteException)6 Fingerprint (android.hardware.fingerprint.Fingerprint)5 MemoryInfo (android.os.Debug.MemoryInfo)5 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)4 Context (android.content.Context)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 Build (android.os.Build)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 SuppressLint (android.annotation.SuppressLint)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 Drawable (android.graphics.drawable.Drawable)2 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2