I need best solution. When I received a push notification, I want to know if my app is open or not, because if my application is open when the user onClick the notification I want to call specific method, but if the application is close I want to open the application.
Asked
Active
Viewed 1,209 times
2 Answers
0
Basically what you need is way to check if one of your activity is in Foreground. May be you should check this How to determine if one of my activities is in the foreground

Community
- 1
- 1

Abhishek Sinha
- 435
- 4
- 12
-1
//Use this method to check your app is running or not
public boolean isAppRunning(){
String packageName="Your package name";
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfoslist = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfoslist.size(); i++)
{
if(procInfoslist.get(i).processName.equals(packageName))
{
return true;
}
}
return false;
}
`
//Use this method to check any activity is running or not
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}

Jinesh Francis
- 1
- 1
-
Welcome to stackoverflow!! please could you post some description about your answer. Thank you. – Robert Apr 26 '16 at 22:49