Android 判断app是否在前台运行

<!-- 前台还是后台 -->
<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     }

 

posted @ 2015-04-24 16:26  鲸歌  阅读(356)  评论(0编辑  收藏  举报