I'm trying to display a notification (if you click on it, opens an Activity with Dialog) if App is closed or running on background, but if it is open or running on foreground I want to show the Dialog directly.
I have this code:
@Override
public void onReceive( Context context, Intent intent )
{
if ( RunningHelper.isAppRunning(context, Constants.PROJECT_NAME ) )
{
Log.d("Running", "--------APP RUNNING");
}
else
{
Log.d("Running", "--------APP NOT RUNNING");
showNotification();
}
}
My RunningHelper:
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;
}
The problem is the App always detect that is running onReceive(), Have someone any clue for do it correctly? Thanks.