Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;

import java.util.List;

public class Main {

    public static String getTopPackageName(Context context) {
        String topPackageName = null;
        try {
            ActivityManager activityManager = (ActivityManager) (context
                    .getSystemService(android.content.Context.ACTIVITY_SERVICE));
            List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
            if (runningTaskInfos != null) {
                ComponentName f = runningTaskInfos.get(0).topActivity;
                topPackageName = f.getPackageName();
            }
        } catch (Exception e) {
        }
        return topPackageName;
    }
}