Skip to content

Instantly share code, notes, and snippets.

@NLMartian
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NLMartian/ef5a9b44902594aadb68 to your computer and use it in GitHub Desktop.
Save NLMartian/ef5a9b44902594aadb68 to your computer and use it in GitHub Desktop.
判断应用程序是否在前台
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teambition.today" >
<uses-permission android:name="android.permission.GET_TASKS" />
</manifest>
private static boolean isInForeground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
return context.getApplicationContext().getPackageName().equals(foregroundTaskPackageName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment