Deepali Deepali - 5 months ago 131
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

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;