Example usage for android.content.pm PackageManager getApplicationLabel

List of usage examples for android.content.pm PackageManager getApplicationLabel

Introduction

In this page you can find the example usage for android.content.pm PackageManager getApplicationLabel.

Prototype

public abstract CharSequence getApplicationLabel(ApplicationInfo info);

Source Link

Document

Return the label to use for this application.

Usage

From source file:Main.java

/***
 * get application name from package name
 * @param context//from www.ja  va 2 s  .c  o  m
 * @param packageName
 * @return
 */
public static String getApplicationName(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo(packageName, 0);
    } catch (final NameNotFoundException e) {
        ai = null;
    }
    String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    return applicationName;
}

From source file:Main.java

public static String getAppName(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo appInfo = null;/*from  w  ww  .j av a 2s .c  o  m*/
    try {
        appInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {

    }
    return appInfo == null ? "null" : packageManager.getApplicationLabel(appInfo).toString();
}

From source file:Main.java

public static String getAppNameFromPkg(Context context, String pkg) {
    final PackageManager pm = context.getPackageManager();
    ApplicationInfo ai;/* w ww  .  ja v a2s.  c o m*/
    try {
        ai = pm.getApplicationInfo(pkg, 0);
    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }
    String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : pkg);
    return applicationName;
}

From source file:io.lqd.sdk.gcm.LQMessageHandler.java

private static String getAppName(Context context) {
    PackageManager manager = context.getPackageManager();
    try {//ww  w .  j a v a2  s. co  m
        ApplicationInfo appinfo = manager.getApplicationInfo(context.getPackageName(), 0);
        return manager.getApplicationLabel(appinfo).toString();
    } catch (PackageManager.NameNotFoundException e) {
        return "";
    }
}

From source file:Main.java

/**
 * //from   w  ww . j  a v a 2  s. c o  m
 * @param c
 * @param uid
 * @return
 * @throws JSONException
 */
public static JSONArray getPackagesForUid(Context c, int uid) throws JSONException {
    PackageManager pm = c.getPackageManager();
    JSONArray res = new JSONArray();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null) {
        for (int i = 0; i < pkgs.length; i++) {
            try {
                CharSequence appLabel = pm
                        .getApplicationLabel(pm.getApplicationInfo(pkgs[i], PackageManager.GET_META_DATA));
                JSONObject pkg = new JSONObject();
                pkg.put("package", pkgs[i]);
                pkg.put("app_label", appLabel.toString());
                res.put(pkg);
            } catch (NameNotFoundException e) {
            } catch (Exception e) {
            }
        }
    }
    return res;
}

From source file:Main.java

public static String getAppName(Context c, int uid, boolean withUid) {
    if (sSystemUids.containsKey(uid)) {
        return sSystemUids.get(uid);
    }//from  w w  w.ja v a  2s.  c om

    PackageManager pm = c.getPackageManager();
    String appName = "Unknown";
    String[] packages = pm.getPackagesForUid(uid);

    if (packages != null) {
        try {
            if (packages.length == 1) {
                appName = pm.getApplicationLabel(pm.getApplicationInfo(packages[0], 0)).toString();
            } else if (packages.length > 1) {
                appName = "";
                for (int i = 0; i < packages.length; i++) {
                    appName += packages[i];
                    if (i < packages.length - 1) {
                        appName += ", ";
                    }
                }
            }
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }
    } else {
        Log.e(TAG, "Package not found for uid " + uid);
    }

    if (withUid) {
        appName += " (" + uid + ")";
    }

    return appName;
}

From source file:Main.java

public static String GetAppName(Activity act) {
    final PackageManager pm = act.getApplicationContext().getPackageManager();

    ApplicationInfo ai;/*from  ww w  . j  a  va 2s .c o  m*/
    try {
        ai = pm.getApplicationInfo(act.getPackageName(), 0);
    } catch (final NameNotFoundException e) {
        ai = null;
    }
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "unknown");
    return applicationName;
}

From source file:io.lqd.sdk.LQPushHandler.java

private static String getAppName(Context context) {
    PackageManager manager = context.getPackageManager();
    try {/*w  w w.j a va  2 s . c o m*/
        ApplicationInfo appinfo = manager.getApplicationInfo(context.getPackageName(), 0);
        return manager.getApplicationLabel(appinfo).toString();
    } catch (NameNotFoundException e) {
        return "";
    }
}

From source file:Main.java

/**
 * @param ctx The Android application context.
 * @return Application name/* w w  w.ja v a 2 s  .  c o  m*/
 */
public static String getAppName(Context ctx) {
    PackageManager packageManager = ctx.getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager.getApplicationInfo(ctx.getApplicationInfo().packageName, 0);
    } catch (final NameNotFoundException ignored) {
    }
    return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}

From source file:Main.java

public static String getAppName(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {/*from  w  w  w .  ja  v a 2s .  c  o m*/
        applicationInfo = packageManager.getApplicationInfo(context.getApplicationInfo().packageName, 0);
    } catch (final PackageManager.NameNotFoundException e) {
    }

    return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}