<!-- 前台還是背景 -->
<uses-permission android:name="android.permission.GET_TASKS" />
1 private boolean isForeground(Context context) {
2 ActivityManager am = (ActivityManager) context
3 .getSystemService(Context.ACTIVITY_SERVICE);
4 ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
5 String currentPackageName = cn.getPackageName();
6 if (!TextUtils.isEmpty(currentPackageName)
7 && currentPackageName.equals(context.getPackageName())) {
8 Log.i("yy", "處于前台");
9 return true;
10 }
11 Log.i("yy", "處于背景");
12 return false;
13 }
轉載于:https://www.cnblogs.com/jinglecode/p/4453816.html