简体   繁体   中英

Android, Learn about the application in the foreground

I want to know if it is possible to know the application in use by the user. Currently, I manage to get the list of active processes on the foreground but unfortunately it does not isolate the specific application for ca also taking applications in the status bar.

Here's what I use:

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses)
    {
        if(appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
        {
            Log.d("Executed app", "Foreground " +appProcess.processName + "\t\t ID: " + appProcess.pid);
        }
    }

Here is what I get:

11-09 18:12:56.645 8821-8821/company.test D/Executed app﹕ Foreground company.gillot.test ID: 8821 11-09 18:12:56.645
8821-8821/company.test D/Executed app﹕ Foreground android.process.acore ID: 30279 11-09 18:12:56.645
8821-8821/company.test D/Executed app﹕ Foreground com.android.phone
ID: 197 11-09 18:12:56.645 8821-8821/company.test D/Executed app﹕ Foreground system ID: 97

Anyone have an idea? I browse the forums and the android api but so far nothing specific property.

BONUS:

Thereafter I would avoid detected applications "system".

Thank you in advance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM