<!-- 前台还是后台 -->
<uses-permission android:name="android.permission.GET_TASKS" />
private boolean isForeground(Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if (!TextUtils.isEmpty(currentPackageName)
&& currentPackageName.equals(context.getPackageName())) {
Log.i("yy", "处于前台");
return true;
}
Log.i("yy", "处于后台");
return false;
}