android

android 判断当前App处于前台还是后台状态运行

位置:首页 > android > 开发技巧,2016-05-10
判断当前App处于前台还是后台状态public static boolean isApplicationBackground( final Context co

判断当前App处于前台还是后台状态

public static boolean isApplicationBackground( final Context context) {

     ActivityManager am = (ActivityManager) context

         .getSystemService(Context.ACTIVITY_SERVICE);

     @SuppressWarnings ( "deprecation" )

     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 ;

   }

需要添加权限

&lt;uses-permission

    android:name= "android.permission.GET_TASKS" />


TAGS:android

猜你喜欢

NewHot