4

So we all know that the getRecentTasks() and getRunningTasks() on ActivityManager are now deprecated and will return a reduced result set on Android L and higher devices.

Alternative to getRunningTasks in Android L

https://code.google.com/p/android-developer-preview/issues/detail?id=29

However, I am trying to find a solution to keep my App Locker app alive on Android L. I need the package name of the top Activity in order to show the lock screen when the users opens/launches the locked app.

It is very similar to this app: https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en

Currently I am using this code:

  ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
                .getRunningTasks(1);
        ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
        String activityOnTop = ar.topActivity.getPackageName();

But it won't work in Android L, so I am not sure what exactly to do...

How can I implement something like this in Android L?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81
  • 3
    Isn't the whole reason they removed this to prevent people from writing apps which interfere with others, ie, **to stop exactly what you are trying to do**? – Chris Stratton Oct 21 '14 at 16:47
  • Ty this: http://stackoverflow.com/questions/26400469/alternative-to-getrunningtasks-in-android-l – Kenumir Oct 30 '14 at 08:48
  • please see my answer in another thread: http://stackoverflow.com/a/27140347/847478 – sunxin8086 Nov 26 '14 at 02:46

3 Answers3

1

Unfortunately, there is no equivalent in Android 5.0: it is not possible to get the top most activity, nor is it possible get any callback when a new application/activity is launched in realtime.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

please refer this link.

i hope it will helpful to you, in my project it work. thanks.

Community
  • 1
  • 1
Vishal Butani
  • 356
  • 4
  • 12
  • 1
    How to get class name to get topactivity – Mukesh Jun 19 '15 at 05:45
  • you can not get the top activity class name in Android L but try this code, it work in my project perfectly.. String mClassName = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName(); @muku – Vishal Butani Jun 26 '15 at 05:05
  • getRunningTasks(1) is deprecated ,i think so it doesnt work in Android L – Mukesh Jun 26 '15 at 05:16
-2

Try this line of code for Android L

ActivityManager activityManager = (ActivityManager) getSystemService (Context.ACTIVITY_SERVICE);

String packageName = activityManager.getRunningAppProcesses().get(0).processName;

and please note that this works only for Lollipop devices .. If you want any platform support then add the following code.

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP)
{
String packageName = activityManager.getRunningAppProcesses().get(0).processName;
}
else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
{
String packageName =  ProcessManager.getRunningForegroundApps(getApplicationContext()).get(0).getPackageName();
}
else
{
String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();

}
AmAnDroid
  • 246
  • 2
  • 11