Created
September 10, 2013 09:07
-
-
Save shaobin0604/6506860 to your computer and use it in GitHub Desktop.
decide whether XXX Activity is run or foreground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 139 private boolean isXXXUiExist(Context context) { | |
| 140 String packageName = context.getPackageName(); | |
| 141 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
| 142 try { | |
| 143 List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(100); | |
| 144 if (runningTasks == null || runningTasks.isEmpty()) { | |
| 145 return false; | |
| 146 } | |
| 147 for (RunningTaskInfo info : runningTasks) { | |
| 148 if (packageName.equals(info.baseActivity.getPackageName())) { | |
| 149 return true; | |
| 150 } | |
| 151 } | |
| 152 return false; | |
| 153 } catch (SecurityException e) { | |
| 154 // assume dolphin is running | |
| 155 return true; | |
| 156 } | |
| 157 } | |
| 158 /** | |
| 159 * Check if XXX UI is foreground | |
| 160 * | |
| 161 * @param context | |
| 162 * @return true if is foreground, false otherwise | |
| 163 */ | |
| 164 private boolean isXXXForeground(Context context) { | |
| 165 String packageName = context.getPackageName(); | |
| 166 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
| 167 try { | |
| 168 List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1); | |
| 169 if (runningTasks == null || runningTasks.isEmpty()) { | |
| 170 return false; | |
| 171 } | |
| 172 RunningTaskInfo runningTaskInfo = runningTasks.get(0); | |
| 173 boolean isForeground = packageName.equals(runningTaskInfo.topActivity.getPackageName()); | |
| 174 return isForeground; | |
| 175 } catch (SecurityException e) { | |
| 176 // assume dolphin is running | |
| 177 return true; | |
| 178 } | |
| 179 } | |
| /** | |
| 182 * Check if XXX UI is foreground | |
| 183 * | |
| 184 * @param context | |
| 185 * @return true if is foreground, false otherwise | |
| 186 */ | |
| 187 private boolean isXXXForeground2(Context context) { | |
| 188 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
| 189 List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses(); | |
| 190 if (runningAppProcesses == null) { | |
| 191 return false; | |
| 192 } | |
| 193 | |
| 194 String packageName = context.getPackageName(); | |
| 195 for (RunningAppProcessInfo appProcessInfo : runningAppProcesses) { | |
| 196 if (packageName.equals(appProcessInfo.processName) && appProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { | |
| 197 return true; | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 return false; | |
| 202 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment