android.app.ActivityManager.RunningAppProcessInfo Java Examples

The following examples show how to use android.app.ActivityManager.RunningAppProcessInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StopListennerActivity.java    From AssistantBySDK with Apache License 2.0 8 votes vote down vote up
private boolean isAppOnForeground() { 
    ActivityManager aManager=((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE));
       List<RunningAppProcessInfo> appProcesses = aManager.getRunningAppProcesses(); 
       if (appProcesses == null) return false; 
       for (RunningAppProcessInfo appProcess : appProcesses) { 
           if (appProcess.processName.equals(getPackageName())){
           	    Log.e("StopListennerActivity", "appProcess.importance="+appProcess.importance);
           	   // inRunningTasks(aManager);
           	    if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 
           			return true; 
           		}else{
           			return false;
           		}
           } 
       } 
       return false; 
}
 
Example #2
Source File: StorageUtil.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
public static void cleanMemory(Context context){
    System.out.println("---> 清理前可用内存:"+getAvailMemory(context)+"MB");
    ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses();

    if (processList != null) {
        for (int i = 0; i < processList.size(); ++i) {
            RunningAppProcessInfo runningAppProcessInfo= processList.get(i);
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE
            // 的进程即为长时间未使用进程或者空进程
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE
            // 的进程都是非可见进程,即在后台运行
            if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = runningAppProcessInfo.pkgList;
                for (int j = 0; j < pkgList.length; ++j) {
                    System.out.println("===> 正在清理:"+pkgList[j]);
                    activityManager.killBackgroundProcesses(pkgList[j]);
                }
            }

        }
    }
    System.out.println("---> 清理后可用内存:"+getAvailMemory(context)+"MB");
}
 
Example #3
Source File: PushNotificationReceiver.java    From letv with Apache License 2.0 6 votes vote down vote up
@SuppressLint({"NewApi"})
public static boolean isAppOnForeground(Context mContext) {
    ActivityManager activityManager = (ActivityManager) mContext.getSystemService("activity");
    String packageName = mContext.getPackageName();
    LogInfo.log("PushReceiver", "packageName =" + packageName);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        LogInfo.log("PushReceiver", "------appProcesses == null-----");
        return false;
    }
    for (RunningAppProcessInfo appProcess : appProcesses) {
        LogInfo.log("PushReceiver", "------appProcess.processName =" + appProcess.processName);
        if (appProcess.processName.equals(packageName) && appProcess.importance == 100) {
            for (RunningTaskInfo rti : activityManager.getRunningTasks(100)) {
                if (!(rti == null || rti.baseActivity == null || mContext.getPackageName() == null || !mContext.getPackageName().equals(rti.baseActivity.getPackageName()) || VERSION.SDK_INT < 11)) {
                    activityManager.moveTaskToFront(rti.id, 1);
                }
            }
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: RNPushNotificationListenerService.java    From react-native-push-notification-CE with MIT License 6 votes vote down vote up
private boolean isApplicationInForeground() {
    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    if (processInfos != null) {
        for (RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.processName.equals(getApplication().getPackageName())) {
                if (processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String d : processInfo.pkgList) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example #5
Source File: FingerprintService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private boolean isForegroundActivity(int uid, int pid) {
    try {
        List<RunningAppProcessInfo> procs =
                ActivityManager.getService().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_SERVICE) {
                return true;
            }
        }
    } catch (RemoteException e) {
        Slog.w(TAG, "am.getRunningAppProcesses() failed");
    }
    return false;
}
 
Example #6
Source File: Utils.java    From styT with Apache License 2.0 6 votes vote down vote up
/**
 * 判断进程是否运行
 * @return
 */
public static boolean isProessRunning(Context context, String proessName) {

	boolean isRunning = false;
	ActivityManager am = (ActivityManager) context
			.getSystemService(Context.ACTIVITY_SERVICE);

	List<RunningAppProcessInfo> lists = am.getRunningAppProcesses();
	for (RunningAppProcessInfo info : lists) {
		if (info.processName.equals(proessName)) {
			isRunning = true;
		}
	}

	return isRunning;
}
 
Example #7
Source File: ProcessFragment.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
public void toggleChecked(final boolean b) {
	if (b) {
		selectedInList1.clear();
		for (RunningAppProcessInfo r : display_process) {
			selectedInList1.add(r.processName);
		}
		if (selectedInList1.size() > 0 && commands.getVisibility() == View.GONE) {
			commands.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.grow_from_bottom));
			commands.setVisibility(View.VISIBLE);
			horizontalDivider6.setVisibility(View.VISIBLE);
		}
	} else {
		selectedInList1.clear();
		if (commands.getVisibility() == View.VISIBLE) {
			horizontalDivider6.setVisibility(View.GONE);
			commands.setAnimation(AnimationUtils.loadAnimation(activity, R.anim.shrink_from_top));
			commands.setVisibility(View.GONE);
		}
	}
	notifyDataSetChanged();
}
 
Example #8
Source File: SystemTool.java    From Lay-s with MIT License 6 votes vote down vote up
/**
 * 判断当前应用程序是否后台运行
 */
public static boolean isBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                // 后台运行
                return true;
            } else {
                // 前台运行
                return false;
            }
        }
    }
    return false;
}
 
Example #9
Source File: AppUtils.java    From SmartChart with Apache License 2.0 6 votes vote down vote up
/**
 * whether this process is named with processName
 *
 * @param context     上下文
 * @param processName 进程名
 * @return <ul>
 * return whether this process is named with processName
 * <li>if context is null, return false</li>
 * <li>if {@link ActivityManager#getRunningAppProcesses()} is null,
 * return false</li>
 * <li>if one process of
 * {@link ActivityManager#getRunningAppProcesses()} is equal to
 * processName, return true, otherwise return false</li>
 * </ul>
 */
public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfoList = manager
            .getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (RunningAppProcessInfo processInfo : manager
            .getRunningAppProcesses()) {
        if (processInfo.pid == pid
                && processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}
 
Example #10
Source File: CCUtil.java    From CC with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前进程的名称
 * @return 进程名
 */
public static String getCurProcessName() {
    if (curProcessName != null) {
        return curProcessName;
    }
    Application application = CC.getApplication();
    if (application == null) {
        return PROCESS_UNKNOWN;
    }
    try {
        ActivityManager manager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
            for (RunningAppProcessInfo appProcess : processes) {
                if (appProcess.pid == android.os.Process.myPid()) {
                    curProcessName = appProcess.processName;
                    return curProcessName;
                }
            }
        }
    } catch (Exception e){
        CCUtil.printStackTrace(e);
    }
    return PROCESS_UNKNOWN;
}
 
Example #11
Source File: CCUtil.java    From CC with Apache License 2.0 6 votes vote down vote up
public static String[] getCurProcessPkgList() {
    Application application = CC.getApplication();
    if (application == null) {
        return null;
    }
    try {
        ActivityManager manager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
            for (RunningAppProcessInfo appProcess : processes) {
                if (appProcess.pid == android.os.Process.myPid()) {
                    return appProcess.pkgList;
                }
            }
        }
    } catch (Exception e){
        CCUtil.printStackTrace(e);
    }
    return null;
}
 
Example #12
Source File: FDroidApp.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Asks if the current process is "org.fdroid.fdroid:acra".
 * <p>
 * This is helpful for bailing out of the {@link FDroidApp#onCreate} method early, preventing
 * problems that arise from executing the code twice. This happens due to the `android:process`
 * statement in AndroidManifest.xml causes another process to be created to run
 * {@link org.fdroid.fdroid.acra.CrashReportActivity}. This was causing lots of things to be
 * started/run twice including {@link CleanCacheService} and {@link WifiStateChangeService}.
 * <p>
 * Note that it is not perfect, because some devices seem to not provide a list of running app
 * processes when asked. In such situations, F-Droid may regress to the behaviour where some
 * services may run twice and thus cause weirdness or slowness. However that is probably better
 * for end users than experiencing a deterministic crash every time F-Droid is started.
 */
private boolean isAcraProcess() {
    ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
    if (processes == null) {
        return false;
    }

    int pid = android.os.Process.myPid();
    for (RunningAppProcessInfo processInfo : processes) {
        if (processInfo.pid == pid && ACRA_ID.equals(processInfo.processName)) {
            return true;
        }
    }

    return false;
}
 
Example #13
Source File: NetUtils.java    From gokit-android with MIT License 6 votes vote down vote up
public static boolean isBackground(Context context) {
	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
	for (RunningAppProcessInfo appProcess : appProcesses) {
		if (appProcess.processName.equals(context.getPackageName())) {
			/*
			 * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000
			 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
			 */
			Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance
					+ ",context.getClass().getName()=" + context.getClass().getName());
			if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				Log.i(context.getPackageName(), "处于后台" + appProcess.processName);
				return true;
			} else {
				Log.i(context.getPackageName(), "处于前台" + appProcess.processName);
				return false;
			}
		}
	}
	return false;
}
 
Example #14
Source File: PluginProcessMain.java    From springreplugin with Apache License 2.0 6 votes vote down vote up
private static final void waitKilled(int pid) {
    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(100, 0);
        } catch (Throwable e) {
            //
        }
        //
        List<RunningAppProcessInfo> processes = AMSUtils.getRunningAppProcessesNoThrows(RePluginInternal.getAppContext());
        if (processes == null || processes.isEmpty()) {
            continue;
        }
        boolean found = false;
        for (RunningAppProcessInfo info : processes) {
            if (info.pid == pid) {
                found = true;
            }
        }
        if (!found) {
            return;
        }
    }
}
 
Example #15
Source File: ServiceHelper.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public boolean isAppOnForeground() {
    // Returns a list of application processes that are running on the
    // device
    String packageName = context.getPackageName();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;
    for (RunningAppProcessInfo appProcess : appProcesses) {
        // importance:
        // The relative importance level that the system places
        // on this process.
        // May be one of IMPORTANCE_FOREGROUND, IMPORTANCE_VISIBLE,
        // IMPORTANCE_SERVICE, IMPORTANCE_BACKGROUND, or IMPORTANCE_EMPTY.
        // These constants are numbered so that "more important" values are
        // always smaller than "less important" values.
        // processName:
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}
 
Example #16
Source File: NetUtils.java    From GOpenSource_AppKit_Android_AS with MIT License 6 votes vote down vote up
public static boolean isBackground(Context context) {
	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
	for (RunningAppProcessInfo appProcess : appProcesses) {
		if (appProcess.processName.equals(context.getPackageName())) {
			/*
			 * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000
			 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
			 */
			Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance
					+ ",context.getClass().getName()=" + context.getClass().getName());
			if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				Log.i(context.getPackageName(), "处于后台" + appProcess.processName);
				return true;
			} else {
				Log.i(context.getPackageName(), "处于前台" + appProcess.processName);
				return false;
			}
		}
	}
	return false;
}
 
Example #17
Source File: api_o.java    From styT with Apache License 2.0 6 votes vote down vote up
private void updateProcessInfo() {
    String mText = "";
    mTextView.setText(mText);

    // 获取ActivityManager
    ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);

    // 获取进程信息***************************************************
    List<RunningAppProcessInfo> infos = activityManager.getRunningAppProcesses();

    for (RunningAppProcessInfo info : infos) {
        String name = info.processName;
        int uid = info.uid;
        int pid = info.pid;

        mText = mTextView.getText().toString();
        mText += name + "\n\n";
        mTextView.setText(mText + " uid=" + uid + " pid=" + pid);

    }

}
 
Example #18
Source File: BigTestUtils.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the memory usage of MyTracks process. This method would get the Pss
 * memory. Pss is the amount of memory shared with other processes, accounted
 * in a way that the amount is divided evenly between the processes that share
 * it. This is memory that would not be released if the process was
 * terminated, but is indicative of the amount that this process is
 * "contributing" to the overall memory load.
 * 
 * @param context
 */
public static String getMyTracksPssMemoryInfo(Context context) {
  int MyTracksProcessId = -1;
  ActivityManager activityManager = (ActivityManager) context
      .getSystemService(Activity.ACTIVITY_SERVICE);
  List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();

  for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
    if (runningAppProcessInfo.processName.equalsIgnoreCase(MYTRACKS_PROCESS_NAME)) {
      MyTracksProcessId = runningAppProcessInfo.pid;
      break;
    }
  }
  if (MyTracksProcessId == -1) {
    Assert.fail();
  }
  int pids[] = { MyTracksProcessId };
  android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
  int memoryUsage = memoryInfoArray[0].getTotalPss();
  return String.format(Locale.US, " MyTracks TotalPss Memory: %d (kB) ", memoryUsage);
}
 
Example #19
Source File: PackageManagerUtil.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 程序是否在前台
 * @deprecated 注:如果对于APP启动后有常驻Service,也判断为了前台(也不准)
 * @param context
 */
@Deprecated
public static boolean isAppOnForeground(final Context context) {
    if (context == null) {
        return false;
    }
    Context appContext = context.getApplicationContext();
    ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager == null) {
        return false;
    }
    String packageName = appContext.getPackageName();
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses != null) {
        for (RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
    }
    return false;
}
 
Example #20
Source File: PackageManagerUtil.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 获取进程的名称<br/>
 * 通过进程id在运行的栈中查找获取进程名称(包名+进程名)
 * 
 * @param context
 * @param pid
 * @return
 */
public static String getProcessNameByPid(Context context, final int pid) {
    String processName = null;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager == null) {
        return "";
    }
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses != null) {
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (pid == appProcess.pid) {
                processName = appProcess.processName;
                break;
            }
        }
    }
    CommonLog.d(TAG, "getProcessNameByPid,pid=", pid, ",processName=", processName);
    return processName;
}
 
Example #21
Source File: PackageManagerUtil.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 检查当前进程名是否是包含进程名的进程
 * 
 * @param context
 * @return
 */
public static boolean checkTheProcess(final Context context, String endProcessName) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    int myPid = android.os.Process.myPid();
    if (activityManager == null) {
        return false;
    }
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses != null) {
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (myPid == appProcess.pid) {
                CommonLog.d(TAG, "process.pid appProcess.processName=" + appProcess.processName + ", endProcessName=" + endProcessName);
                if (appProcess.processName.endsWith(endProcessName)) {
                    return true;
                }
                break;
            }
        }
    }
    return false;
}
 
Example #22
Source File: CheckUtil.java    From ZhihuDaily with MIT License 6 votes vote down vote up
/** APP is not runing true:false  */
public static boolean isBackground(Context context) {
       ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
       for (RunningAppProcessInfo appProcess : appProcesses) {
           //判断所有的app进程,报名是否等于该app
       	if (appProcess.processName.equals(context.getPackageName())) {
               if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                   return true; //"处于后台"
               } else {
                   return false;//"处于前台"
               }
           }
       }
       return false;
   }
 
Example #23
Source File: SystemUtils.java    From VideoMeeting with Apache License 2.0 6 votes vote down vote up
/**
 * 判断当前应用程序是否后台运行
 */
public static boolean isBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                // 后台运行
                return true;
            } else {
                // 前台运行
                return false;
            }
        }
    }
    return false;
}
 
Example #24
Source File: Process.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * Populate running process info into the runningAppInfo WeakReference list
 * and return its value.
 *
 * @param context the Context
 * @return the value of the runningAppInfo WeakReference list after setting it.
 */
public static List<RunningAppProcessInfo> getRunningProcessInfo(Context context) {
    if (runningAppInfo != null && runningAppInfo.get() != null) {
        return runningAppInfo.get();
    }

    ActivityManager manager =
            (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningAppProcesses = manager.getRunningAppProcesses();

    for (RunningAppProcessInfo info : runningAppProcesses) {
        if (info == null || info.processName == null) continue;
        info.processName = StringHelper.formatProcessName(info.processName);
    }

    runningAppInfo = new WeakReference<>(runningAppProcesses);
    return runningAppProcesses;
}
 
Example #25
Source File: Application.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * Helper to query whether an application is currently running
 * and its code has not been evicted from memory.
 *
 * @param context Application's context
 * @param appName The package name or process name of the application.
 * @return true if the application is running, false otherwise.
 */
public static boolean isRunning(final Context context, String appName) {
    List<RunningAppProcessInfo> runningProcessInfo = Process.getRunningProcessInfo(context);
    List<RunningServiceInfo> services = Service.getRunningServiceInfo(context);

    for (RunningAppProcessInfo info : runningProcessInfo) {
        if (info.processName.equals(appName) &&
                info.importance != RunningAppProcessInfo.IMPORTANCE_EMPTY) {
            return true;
        }
    }

    for (RunningServiceInfo service : services) {
        String name = StringHelper.formatProcessName(service.process);
        if (name.equals(appName)) return true;
    }

    return false;
}
 
Example #26
Source File: StringHelper.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
public static String importanceStringLegacy(String description) {
    String importance;
    switch (description) {
        case "system" :
            importance = importanceString(RunningAppProcessInfo.IMPORTANCE_BACKGROUND);
            break;
        case "user" :
            importance = importanceString(Config.IMPORTANCE_APP);
            break;
        case "user-service" :
            importance = importanceString(RunningAppProcessInfo.IMPORTANCE_SERVICE);
            break;
        default :
            importance = "Unknown";
            break;
    }

    return importance;
}
 
Example #27
Source File: NetUtils.java    From Gizwits-SmartBuld_Android with MIT License 6 votes vote down vote up
public static boolean isBackground(Context context) {
	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
	for (RunningAppProcessInfo appProcess : appProcesses) {
		if (appProcess.processName.equals(context.getPackageName())) {
			/*
			 * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000
			 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
			 */
			Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance
					+ ",context.getClass().getName()=" + context.getClass().getName());
			if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				Log.i(context.getPackageName(), "处于后台" + appProcess.processName);
				return true;
			} else {
				Log.i(context.getPackageName(), "处于前台" + appProcess.processName);
				return false;
			}
		}
	}
	return false;
}
 
Example #28
Source File: ChromiumMultiDexInstaller.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private static String getProcessName(Context context) {
    try {
        String currentProcessName = null;
        int pid = android.os.Process.myPid();

        ActivityManager manager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) {
                currentProcessName = processInfo.processName;
                break;
            }
        }

        return currentProcessName;
    } catch (SecurityException ex) {
        return null;
    }
}
 
Example #29
Source File: PluginProcessManager.java    From DroidPlugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #30
Source File: MyActivityManagerService.java    From DroidPlugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void doGc(RunningAppProcessInfo myInfo) {
    int activityCount = mRunningProcessList.getActivityCountByPid(myInfo.pid);
    int serviceCount = mRunningProcessList.getServiceCountByPid(myInfo.pid);
    int providerCount = mRunningProcessList.getProviderCountByPid(myInfo.pid);
    if (activityCount <= 0 && serviceCount <= 0 && providerCount <= 0) {
        //杀死空进程。
        Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s)", myInfo.pid, myInfo.uid, myInfo.processName);
        try {
            android.os.Process.killProcess(myInfo.pid);
        } catch (Throwable e) {
            Log.e(TAG, "error on killProcess", e);
        }
    } else if (activityCount <= 0 && serviceCount > 0 /*&& !mRunningProcessList.isPersistentApplication(myInfo.pid)*/) {
        List<String> names = mRunningProcessList.getStubServiceByPid(myInfo.pid);
        if (names != null && names.size() > 0) {
            for (String name : names) {
                Intent service = new Intent();
                service.setClassName(mHostContext.getPackageName(), name);
                AbstractServiceStub.startKillService(mHostContext, service);
                Log.i(TAG, "doGc kill process(pid=%s,uid=%s processName=%s) service=%s", myInfo.pid, myInfo.uid, myInfo.processName, service);
            }
        }
    }
}