Example usage for android.content Context getPackageName

List of usage examples for android.content Context getPackageName

Introduction

In this page you can find the example usage for android.content Context getPackageName.

Prototype

public abstract String getPackageName();

Source Link

Document

Return the name of this application's package.

Usage

From source file:Main.java

public static <T> T getMetaData(Context context, String name) {
    try {//from w  w w  . j  a  v  a2s. c o m
        final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);

        if (ai.metaData != null) {
            return (T) ai.metaData.get(name);
        }
    } catch (Exception e) {
        System.out.print("Couldn't find meta-data: " + name);
    }

    return null;
}

From source file:Main.java

public static String getApplicationName(Context context) {
    Resources appR = context.getResources();
    String txt = (String) appR.getText(appR.getIdentifier("app_name", "string", context.getPackageName()));
    return txt;/*from   w w  w.j av  a 2 s .com*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T getMetaData(Context context, String name) {
    try {/*from  w  w w.  java2 s.c  om*/
        final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);

        if (ai.metaData != null) {
            return (T) ai.metaData.get(name);
        }
    } catch (Exception e) {
        Log.w("Couldn't find:" + name, name);
    }

    return null;
}

From source file:Main.java

/**
 * Get application name.//  www.  jav a  2  s.  c o  m
 * 
 * @return
 */
public static String getApplicationName(Context context) {
    String name = "?";
    try {
        PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        name = context.getString(pi.applicationInfo.labelRes);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Package name not found", e);
    }
    ;
    return name;
}

From source file:Main.java

public static String getVersionName(Context ctx, boolean withVersionCode) {
    try {/*w  w w  .j a  v  a 2s  .  c  om*/
        PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
        if (withVersionCode) {
            return pi.versionName + " (" + pi.versionCode + ")";
        } else {
            return pi.versionName;
        }
    } catch (NameNotFoundException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static String getVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    try {/*from w ww  .ja  va  2  s .co  m*/
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode + "";
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    try {/*  w ww.  j  a va 2  s  .  co m*/
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return pi == null ? -1 : pi.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static int getAppVersion(Context context) {
    int version = 0;
    try {//from  w ww.  ja  va2s  .c  o  m
        version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {

        e.printStackTrace();
    }
    return version;

}

From source file:Main.java

public static int getVersionCode(Context app) {
    PackageManager pm = app.getPackageManager();
    PackageInfo pi;/*w ww  . j a v a2  s.co  m*/
    try {
        pi = pm.getPackageInfo(app.getPackageName(), 0);
        return pi.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
}