Mohammadreza Yektamaram
Mohammadreza Yektamaram

Reputation: 1502

How can i check if an app is open in Android?

I see that question: How can i check if an app running in Android?

But i want to get name of opened application ( user see ) live in android.

I want to run a background service to check that.

Upvotes: 0

Views: 2456

Answers (1)

Mateus Brandao
Mateus Brandao

Reputation: 900

You can get the running Application package name using this function:

public String foregroundPackage() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
        ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
        packageName = componentInfo.getPackageName();
        return componentInfo.getPackageName();
    }

Upvotes: 2

Related Questions