0

I was try it but while api level above 20 i was not getting response. (response getting null).

ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String activityOnTop;
if (Build.VERSION.SDK_INT > 20) {

    activityOnTop = mActivityManager.getRunningAppProcesses().get(1).processName;
} else {
    List<ActivityManager.RunningTaskInfo> RunningTask1 = mActivityManager
            .getRunningTasks(1);
    ActivityManager.RunningTaskInfo ar1 = RunningTask1.get(0);
    activityOnTop = ar1.topActivity.getPackageName();
}

Log.e("activityOnTop", "!!!!!!!!!!" + activityOnTop);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Umesh
  • 1
  • 1
  • This has been asked before but basically you have to keep track of which Activity have been resumed/paused etc yourself – David Rawson May 12 '17 at 09:53
  • Ok David Rawson but it's nor possible. – Umesh May 12 '17 at 10:25
  • 4
    Check this question: [How to check if my activity is the current activity running in the screen](http://stackoverflow.com/questions/3262157/how-to-check-if-my-activity-is-the-current-activity-running-in-the-screen) – David Rawson May 12 '17 at 10:28
  • I think you didn't understand my quations. proper David Rawson bcz i want a package name of other opened apps. not mine. i.e. Demo it's my application name now i am calling service call from my demo apps. right. now close the demo apps. and open instagram. So, now in my service call i want package name of instagram. – Umesh May 12 '17 at 10:41
  • Part of the problem is the way you have named your variable (`activityOnTop`). But still, try this question: [how to find the currently running applications programatically in Android](http://stackoverflow.com/questions/8091350/how-to-find-the-currently-running-applications-programatically-in-android) – David Rawson May 12 '17 at 10:46
  • it's work fine. but api level below of 20. – Umesh May 12 '17 at 11:20

0 Answers0