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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations