Deepali Deepali - 2 years ago 408
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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download