Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

ActivityManager am = (ActivityManager) aContext
                .getSystemService(Context.ACTIVITY_SERVICE);            
List<ActivityManager.RunningTaskInfo> 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.

like image 366
Krishna Avatar asked Dec 29 '14 12:12

Krishna


People also ask

How can I get current activity?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view to show activity name.

What is activity tag android?

<activity> activity is the subelement of application and represents an activity that must be defined in the AndroidManifest. xml file. It has many attributes such as label, name, theme, launchMode etc. android:label represents a label i.e. displayed on the screen.

Where do you define the starting activity of your application?

Every activity that the android app uses must be declared in the AndroidManifest. xml file. and the main activity for the app must be declared in the manifest with a <intent-filter> that includes the MAIN action and LAUNCHER.


2 Answers

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<ActivityManager.RunningTaskInfo> 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<String> activePackages = new HashSet<String>();
  final List<ActivityManager.RunningAppProcessInfo> 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 :)

like image 170
Ashish Tamrakar Avatar answered Nov 02 '22 11:11

Ashish Tamrakar


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
like image 38
Ismael Avatar answered Nov 02 '22 12:11

Ismael