How to get package names for Android L

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

Alternative getRunningTasks in Android L

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

However, I am trying to find a solution that supports the App Locker application on Android L. I need the name of the top-action package to show the lock screen when users open / launch a locked application.

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

I am currently 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 will not work in Android L, so I'm not sure what exactly to do ...

- Android L?

+4
3

, Android 5.0 : , / .

+1

. .

, , . .

+1

Android L

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

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

, Lollipop. , .

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();

}
-1
source

All Articles