I realize this question has been asked many times in a various way, however, all of these answers given are from roughly 5-7 years ago. With the new android API's, most of these answers are methods given which have been depreciated.
I know it can be done, because there are many apps on the play store that can do it, like Appblock which recognizes an app opening and then going back to the home page, or AppLocker where it recognizes an app that opens and creates a pin lock overlay.
Both of these apps are examples of a service used to identify the package name of the current activity that is on top.
Now, How can I get the package name of an app that opens so I can use it in a service which goes to the home screen if a specific app is open.
I'm working on an android app that allows the user to check off what apps to block from opening, and then if the app is opening just send the user to the home screen.
here are a few of the bunch of stack overflow questions I've gone through(I've been searching for an answer for this for a week)
How to get package name from anywhere?
how to getTopActivity name or get current running application package name in lollipop?
Android: How can I get current opened application name on screen
all of these answers don't help because they use depreciated methods in the new Android API's