PHP
·
发表于 5年以前
·
阅读量:8391
public static boolean isApplicationBackground( final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks( 1 );
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get( 0 ).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true ;
}
}
return false ;
}
<uses-permission android:name= "android.permission.GET_TASKS" />