Deepali Deepali - 1 year ago 328
Android Question

How to get current foreground activity context in android?

Whenever my broadcast is executed I want to show alert to foreground activity.

Answer Source

Knowing that ActivityManager manages Activity, so we can gain information from ActivityManager. We get the current foreground running Activity by

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;