一、判断前台后台
转载链接:http://www.jb51.net/article/45084.htm
1、通过RunningTaskInfo类判断(需要额外权限):

<uses-permission android:name="android.permission.GET_TASKS" />  
/** *判断当前应用程序处于前台还是后台 */    public static boolean isApplicationBroughtToBackground(final Context context) {        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        List<RunningTaskInfo> tasks = am.getRunningTasks(1);        if (!tasks.isEmpty()) {            ComponentName topActivity = tasks.get(0).topActivity;            if (!topActivity.getPackageName().equals(context.getPackageName())) {                return true;            }        }        return false;    }

需要在AndroidMenitfest.xml文件中添加以下权限
复制代码 代码如下:


2、通过RunningAppProcessInfo类判断(不需要额外权限):
复制代码 代码如下:

public static boolean isBackground(Context context) {    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();    for (RunningAppProcessInfo appProcess : appProcesses) {         if (appProcess.processName.equals(context.getPackageName())) {                if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {                          Log.i("后台", appProcess.processName);                          return true;                }else{                          Log.i("前台", appProcess.processName);                          return false;                }           }    }    return false;}

二、获取桌面所有应用

//**     * 获得属于桌面的应用的应用包名称     * @return 返回包含所有包名的字符串列表     *//*    private List<String> getHomes() {        List<String> names = new ArrayList<String>();        PackageManager packageManager = this.getPackageManager();        // 属性        Intent intent = new Intent(Intent.ACTION_MAIN);        intent.addCategory(Intent.CATEGORY_HOME);        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,                PackageManager.MATCH_DEFAULT_ONLY);        for(ResolveInfo ri : resolveInfo) {            names.add(ri.activityInfo.packageName);        }        return names;    }
 *//**     * 判断当前界面是否是桌面     *//*    public boolean isHome(){        if(mActivityManager == null) {            mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);        }        List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);        return homeList.contains(rti.get(0).topActivity.getPackageName());    }

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 如何在后台运行Linux命令?
  3. [置顶] Android开发之将Edittext输入弹出的软键盘设置搜索确定键
  4. 常用的Android(安卓)Widget组件学习②-EditText的介绍和应用。
  5. Android中在WebView中使用javascript
  6. Android(安卓)学习笔记--android基本
  7. Ubuntu 下编译Android(安卓)源代码
  8. someone's android note
  9. Android判断两个时间的间隔

随机推荐

  1. android添加动画效果
  2. Android控件笔记——CheckBox复选框
  3. android GridView android:stretchMode="
  4. Android开发神贴整理
  5. Android Manifest merger failed :tools:r
  6. android 编译
  7. Android中的Service 与 Thread 的区别
  8. Android系统中自带的图标
  9. Android编译过程详解(二)
  10. Android程序员必备精品资源