Check if Activity is running from Service

Mobarak
Posts: 5
Joined: Mon Mar 14, 2016 10:53 am

Check if Activity is running from Service

Postby Mobarak » Mon Mar 14, 2016 12:10 pm

How can a Service check if one of it's application's Activity is running in foreground?

Jalal Ahmed
Posts: 7
Joined: Mon Mar 14, 2016 10:09 am

Re: Check if Activity is running from Service

Postby Jalal Ahmed » Mon Mar 14, 2016 12:15 pm

I have the same issue: A push receiving service needs to decide between poping a notification or some sort of a popup on the app

Saifur
Posts: 6
Joined: Mon Mar 14, 2016 11:02 am

Re: Check if Activity is running from Service

Postby Saifur » Mon Mar 14, 2016 12:21 pm

Use the below method with your package name. It will return true if any of your activities is in foreground.

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


Return to “Android development”

Who is online

Users browsing this forum: No registered users and 1 guest