0

I am developing an app which uses getRunningTasks() function to get the top activity name. But getRunningTasks() is deprecated in Android L. Is there any other way to get the top activity name (not package name) in Lollipop.

android
  • 163
  • 1
  • 15
  • Possible duplicate of [getRunningTasks doesn't work in Android L](http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l) – bummi Dec 18 '15 at 09:13
  • I'm also looking for a way to get the activity name, but just found A LOT of ways to get the package name. Did you find some answer? – Joubert Vasconcelos May 17 '16 at 00:41

3 Answers3

1
Class<?> currentClassName; // Declare Globally 

    Activity activity = (Activity) context;//casting context into activity
    try
    {
        currentClassName = Class.forName(activity.getClass().getName()); //getting the current activity's class name from activity object
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }

Please read SO Answer

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

I found a solution to get top activity name on Lollipop with the new Class UsageStatsManager added in Android L.

First you need the permission

<uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions" />

And the user must enable this option.

Then use this method queryEvents()

There is a simple example.

while (true) {
    final long INTERVAL = 1000;
    final long end = System.currentTimeMillis();
    final long begin = end - INTERVAL;
    final UsageEvents usageEvents = manager.queryEvents(begin, end);
    while (usageEvents.hasNextEvent()) {
        UsageEvents.Event event = new UsageEvents.Event();
        usageEvents.getNextEvent(event);
        if (event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            Log.d("event", "timestamp : " + event.getTimeStamp());
            Log.d("event", "package name : " + event.getPackageName());
            Log.d("event", "class name : " + event.getClassName());
        }
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

When an activity become to foreground, you can see its class name and package name in Logcat.

Tsich'i
  • 139
  • 11
0

AccessibilityService can resolve this requirement, but the Accessibilityservice api in android not stable, sometimes need checkbox off and on again, some code

<application>
<service
    android:name=".service.DetectService"
    android:exported="false"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>

    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_service" />
</service>
<?xml version="1.0" encoding="UTF-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagReportViewIds"
    android:canPerformGestures="true"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="100" />
    
import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

public class DetectService extends AccessibilityService {
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo source = event.getSource();
        if (source == null) {
            return;
        }

        Log.d("DetectService", "type: " + event.getEventType());

        CharSequence packageName = source.getPackageName();
        if (event.getEventType() != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            return;
        }

        ComponentName componentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString());

        try {
            String activityName = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA
                    | PackageManager.MATCH_ALL).toString();
            activityName = activityName.substring(activityName.indexOf(" "), activityName.indexOf("}"));
            Log.e("DetectService", packageName + ": " + activityName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            // sometime getActivityInfo will get NameNotFoundException,try use componentName
        }
    }

    @Override
    public void onInterrupt() {
        Log.w("DetectService", "onInterrupt");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.w("DetectService", "onUnbind");
        return super.onUnbind(intent);
    }
}

z.houbin
  • 216
  • 3
  • 6