Android how to find out which app opens in any instance

In my application, I want to show a message using a BroadcastReceiver, but I want it to display a toast message only when the application is not open and is currently running on the screen (it can run in the background). But I don't find the exact code to fulfill the condition accordingly.

I have tried the code,

    ActivityManager manager = 
        (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();

      

but it also stops the message when another app starts.

Please help me, how can I tell my code if the application is currently open or not? Thank.

+3


source to share


1 answer


With that answer in mind , and finally cyanogenmod 7.

You're halfway there. RunningAppProcessInfo has a pkgList field, String [], which contains all the package names in the process that you can match against to determine if this is your application.



private RunningAppProcessInfo getForegroundApp() {
    RunningAppProcessInfo result=null, info=null;

    if(mActivityManager==null)
        mActivityManager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List <RunningAppProcessInfo> l = mActivityManager.getRunningAppProcesses();
    Iterator <RunningAppProcessInfo> i = l.iterator();
    while(i.hasNext()){
        info = i.next();
        if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                && !isRunningService(info.processName)){
            result=info;
            break;
        }
    }
    return result;

      

}

-1


source







All Articles