3

How to get top activity name in Lollipop?

((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).
   getRunningTasks(1).get(0).topActivity

is not longer available for Lollipop:

     * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
     * is no longer available to third party
     * applications: the introduction of document-centric recents means
     * it can leak person information to the caller.  For backwards compatibility,
     * it will still retu rn a small subset of its data: at least the caller's
     * own tasks, and possibly some other tasks
     * such as home that are known to not be sensitive.

Calling from onResume in MyActivity

MyApplication.getInstance().saveCurentActivity(MyActivity.this)
saveCurentActivity(Activity a) {
    this.activityName = a.getClass().getSimpleName();
}

is no good idea, because MyApplication can be destroyed on error (for example, NPE).

VKDev
  • 604
  • 7
  • 18

3 Answers3

1

This code is working for me.

ActivityManager activityManager = (ActivityManager)    context.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
String currentTopActivity = taskInfo.get(0).topActivity.getClassName();

It will give TopActivity.

user3863488
  • 227
  • 1
  • 13
  • 1
    getRunningTasks is deprecated – VKDev Dec 18 '15 at 12:58
  • 1
    Use getAppTasks() and gettaskinfo instead....like below List tasks = activityManager.getAppTasks(); ActivityManager.RecentTaskInfo taskInfo = tasks.get(0).getTaskInfo(); – user3863488 Dec 18 '15 at 15:14
1

This worked for me:

                long end = System.currentTimeMillis();
                long INTERVAL=2000;
                long begin = end - INTERVAL;
                if (Build.VERSION.SDK_INT >= 21) {
                    UsageStatsManager us = (UsageStatsManager) MyServices.this.getSystemService(Context.USAGE_STATS_SERVICE);
                    UsageEvents usageEvents = us.queryEvents(begin, end);
                    while (usageEvents.hasNextEvent()) {
                        UsageEvents.Event event = new UsageEvents.Event();
                        usageEvents.getNextEvent(event);
                        if (event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
                            Log.e("event", "timestamp : " + event.getTimeStamp());
                            Log.e("event", "package name : " + event.getPackageName());
                            Log.e("event", "Class name : " + event.getClassName());
                            //You can find activity name as Class name      

                        }

                    }
                }
ahmad dehghan
  • 37
  • 1
  • 3
0

I'm not familiar with the original API you are using, but you can track Activity life-cycle changes in your app, using an ActivityLifecycleCallbacks that could be registered via Application.registerActivityLifecycleCallbacks. It would get notified whenever an Activity is created, started, resumed, etc. You might be able to track the current Activity using the onActivityResumed and onActivityPaused hooks.

cwbowron
  • 1,015
  • 6
  • 10
  • good idea, but how I can to store names? Not in MyApplication instance. – VKDev Dec 18 '15 at 12:46
  • What are you doing the with the names? Could you store in the ActivityLifeCycleCallback and make it a singleton or a static instance? – cwbowron Dec 18 '15 at 12:48
  • I need to save current top activity name into variable for using in my logic. Ill try your solution, thanks. – VKDev Dec 18 '15 at 12:51