Determining the current foreground application from a background task or service

后端 未结 13 1434
我寻月下人不归
我寻月下人不归 2020-11-22 02:29

I wish to have one application that runs in the background, which knows when any of the built-in applications (messaging, contacts, etc) is running.

So my questions

13条回答
  •  醉话见心
    2020-11-22 02:47

    This worked for me. But it gives only the main menu name. That is if user has opened Settings --> Bluetooth --> Device Name screen, RunningAppProcessInfo calls it as just Settings. Not able to drill down furthur

    ActivityManager activityManager = (ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE );
                    PackageManager pm = context.getPackageManager();
                    List appProcesses = activityManager.getRunningAppProcesses();
                    for(RunningAppProcessInfo appProcess : appProcesses) {              
                        if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(appProcess.processName, PackageManager.GET_META_DATA));
                            Log.i("Foreground App", "package: " + appProcess.processName + " App: " + c.toString());
                        }               
                    }
    

提交回复
热议问题