Search code examples
javaandroidandroid-serviceandroid-broadcastreceiverandroid-intentservice

Android: Activity overlap another app


I need some help to guide me in a project, i need that my app recognize when another app get started and then my activity shows up. I researched about service, intentservice and broadcastreceiver. But i dont know yet how to execute my idea. Can you guys recommend me some posts, books, tutorials?

P.S.: Im fammiliar with Java and Kotlin


Solution

  • Hmm sounds a bit unethical. However, you could do it.

    First write your service that would run continuously. Then determine how often you would check if the app is running. You would need the GET TASKS permission in your manifest. Then periodically check of running packages like:

    public static boolean isAppRunning(final Context context, final String packageName) {
            final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            if (procInfos != null)
            {
                for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
                    if (processInfo.processName.equals(packageName)) {
                        return true;
                    }
                }
            }
            return false;
        }
    

    Then just call this method periodically from your service with the package you care about. If you see it running then simply create an intent and put the FLAG_ACTIVITY_NEW_TASK in the intent to ensure it starts the activity of your choice.

    The other alternative is to read log cat logs and constantly scan it for the app starting up. Either way it will probably not be instant unless you are always running which could be battery intensive and would eventually warn the user most likely on the battery usage.

    At any rate, that is how you do it, whether it is good or bad, that will work lol.