如何确定,如果Android应用程序正在使用? [英] How to determine, if Android app is currently in use?

查看:120
本文介绍了如何确定,如果Android应用程序正在使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用大量内存时使用它。我想释放内存时,不使用的应用程序。

My app uses a lot of memory when it is used. I would like to free the memory when the app is not used.

我已实施 Application.onTrimMemory()。但这种方法被称为即使我的应用程序正在使用(活动是psented用户$ P $)。如何确定我的应用程序的任何活动是当前活动?

I have implemented Application.onTrimMemory(). But this method is called even if my app is currently in use (an activity is presented to the user). How to determine if any activity of my app is currently active?

的minSdkVersion =9

minSdkVersion="9"

推荐答案

使用这种简单的方法来检查应用程序是否在前台运行,还是不行!

Use this simple method to check whether the app is running on foreground or not!

public boolean isAppForground() {

        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> l = mActivityManager.getRunningAppProcesses();
        Iterator<RunningAppProcessInfo> i = l.iterator();
        while (i.hasNext()) {
            RunningAppProcessInfo info = i.next();

            if (info.uid == getApplicationInfo().uid && info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        return false;
    }

这篇关于如何确定,如果Android应用程序正在使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆