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 android.content.pm.PackageManager;

public class Main {
    static Context collectUtilContext;

    /**
     * @param context
     * @return String
     * @throws
     * @Title: getActivityName
     * @Description: TODO
     */
    public static String getActivityName(Context context) {
        collectUtilContext = context;
        if (collectUtilContext == null) {
            return "";
        }
        ActivityManager am = (ActivityManager) collectUtilContext
                .getSystemService(collectUtilContext.ACTIVITY_SERVICE);
        if (checkPermission(collectUtilContext, "android.permission.GET_TASKS")) {
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            return cn.getShortClassName();
        }
        return "";
    }

    /**
     * @param context
     * @param permission
     * @return boolean
     * @Title: checkPermission
     * @Description: TODO
     */
    public static boolean checkPermission(Context context, String permission) {

        collectUtilContext = context;
        PackageManager pm = context.getPackageManager();
        return pm.checkPermission(permission,
                collectUtilContext.getPackageName()) == PackageManager.PERMISSION_GRANTED;
    }
}