How to get running application activity Name in android 5.0(L)?

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I am using the following code to get the current running activity name in android.

ActivityManager am = (ActivityManager) aContext                 .getSystemService(Context.ACTIVITY_SERVICE);             List alltasks = am                 .getRunningTasks(1);  ComponentName componentInfo = alltasks.get(0).topActivity; componentInfo.getClassName();  System.out.println("Current:"+componentInfo.getClassName()); 

This is working fine in all the versions below android 5.0. But in Android 5.0 it always returning the launcher activity.

Please any one help in this because I want to make run the application in all android versions.

回答1:

Prior to Android L your code will work, but from Android L onward getRunningTask will not work. You have to use getAppRunningProcess.

Check this code below -

public class DetectCalendarLaunchRunnable implements Runnable {  @Override public void run() {   String[] activePackages;   if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {     activePackages = getActivePackages();   } else {     activePackages = getActivePackagesCompat();   }   if (activePackages != null) {     for (String activePackage : activePackages) {       if (activePackage.equals("com.google.android.calendar")) {         //Calendar app is launched, do something       }     }   }   mHandler.postDelayed(this, 1000); }  String[] getActivePackagesCompat() {   final List taskInfo = mActivityManager.getRunningTasks(1);   final ComponentName componentName = taskInfo.get(0).topActivity;   final String[] activePackages = new String[1];   activePackages[0] = componentName.getPackageName();   return activePackages; }  String[] getActivePackages() {   final Set activePackages = new HashSet();   final List processInfos = mActivityManager.getRunningAppProcesses();   for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {     if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {       activePackages.addAll(Arrays.asList(processInfo.pkgList));     }   }   return activePackages.toArray(new String[activePackages.size()]); } }   

Hope this helps you :)



回答2:

You can use this:

String packageName = ""; String className = "";  UsageStatsManager usageStatsManager = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);  if (usageStatsManager != null) {    UsageEvents queryEvents = usageStatsManager.queryEvents(System.currentTimeMillis() - 10000, System.currentTimeMillis());     if (queryEvents != null)     {       UsageEvents.Event event = new UsageEvents.Event();        while (queryEvents.hasNextEvent())        {           UsageEvents.Event eventAux = new UsageEvents.Event();           queryEvents2.getNextEvent(eventAux);            if (eventAux.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND)           {               event = eventAux;           }       }        packageName = event.getPackageName();       className = event.getClassName();    } } 

You need the android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.

Or you can do:

pm grant com.your.package android.permission.PACKAGE_USAGE_STATS 


回答3:

Please Check link.

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.

methods deplicated in 5.0



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!