get Top Activity Class Name With Package - Android Activity

Android examples for Activity:Top Activity

Description

get Top Activity Class Name With Package

Demo Code


//package com.java2s;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;

public class Main {
    public static String getTopActivityClassNameWithPackage(
            final Context context) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            return topActivity.getClassName();
        }/* ww  w  .  j a v  a 2 s  .  c o  m*/
        return null;
    }
}

Related Tutorials