Java tutorial
//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.PackageInfo; import android.content.pm.PackageManager; public class Main { public static String getTopPackageName(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = activityManager.getRunningTasks(1).get(0).topActivity; return cn.getPackageName(); } public static String getPackageName(Context context) { return null == getPackageInfo(context) ? null : getPackageInfo(context).packageName; } public static PackageInfo getPackageInfo(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo info = packageManager.getPackageInfo(context.getPackageName(), 0); return info; } catch (Exception e) { e.printStackTrace(); } return null; } }