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 String getVersion(Context act) {
    PackageManager pm = act.getPackageManager();
    try {//from  w w w .j  ava2s .  c  o  m
        PackageInfo packInfo = pm.getPackageInfo(act.getPackageName(), 0);
        return packInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        // can't reach
        return "";
    }
}

From source file:Main.java

public static String getAppVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    try {//w w  w .  j a v a2  s .  c  om
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        return info.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Retrieves the resource ID from a string (for example, a drawable from the
 * image name)./*from w  ww.  j a  va  2s  . co m*/
 *
 * @param context Application or Activity context
 * @param type Resource type
 * @param resourceName Resource's name
 *
 * @return Resource ID of the named resource
 */
public static int resourceIdFromString(final Context context, final String type, final String resourceName) {
    return context.getResources().getIdentifier(resourceName, type, context.getPackageName());
}

From source file:Main.java

public static PackageInfo getPackageInfo(Context context) {
    PackageInfo packageInfo = null;//from   w ww .  j a v  a  2s.  c om
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException ignore) {
    }
    return packageInfo;
}

From source file:Main.java

private static Class<?> getActivityClass(Context context, Class<?> classObj) {
    StringBuilder b = new StringBuilder(255);
    b.append(context.getPackageName());
    b.append(".");
    b.append(classObj.getSimpleName());//from ww w . j a v a2  s.  c o m
    b.append("Ext");

    String extClassName = b.toString();
    try {
        Class<?> extClass = Class.forName(extClassName);
        return extClass;

    } catch (ClassNotFoundException e) {
        //e.printStackTrace();   // DEBUG: uncomment to debug
        return classObj;
    }
}

From source file:Main.java

/** Returns the value of the versionCode attribute. */
public static int getVersionCode(Context context) {
    int versionCode = 0;
    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();
    try {/*from  w w  w  . ja  va 2  s .c  o  m*/
        versionCode = pm.getPackageInfo(packageName, 0).versionCode;
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e); // Should never happen.
    }
    return versionCode;
}

From source file:Main.java

public static int getCurrVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    try {//w ww  .  ja v  a 2s. co m
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        return info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    String version = "0";
    try {//ww  w  . j  a v  a2s  . c om
        version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("the application not found");
    }
    return version;
}

From source file:Main.java

public static String getVersionName(Context pContext) throws PackageManager.NameNotFoundException {
    PackageInfo pInfo = pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), 0);
    return pInfo.versionName;
}

From source file:Main.java

public static void initializeAnimations(Context ctx) {
    Resources r = ctx.getResources();
    sFadeInAnimationId = r.getIdentifier("fade_in", "anim", ctx.getPackageName());
    sFadeOutAnimationId = r.getIdentifier("fade_out", "anim", ctx.getPackageName());
    sSlideInBottomAnimationId = r.getIdentifier("slide_bottom_in", "anim", ctx.getPackageName());
    sSlideOutBottomAnimationId = r.getIdentifier("slide_bottom_out", "anim", ctx.getPackageName());
    sSlideInTopAnimationId = r.getIdentifier("slide_top_in", "anim", ctx.getPackageName());
    sSlideOutTopAnimationId = r.getIdentifier("slide_top_out", "anim", ctx.getPackageName());
    sSlideInLeftAnimationId = r.getIdentifier("slide_left_in", "anim", ctx.getPackageName());
    sSlideOutLeftAnimationId = r.getIdentifier("slide_left_out", "anim", ctx.getPackageName());
    sSlideInRightAnimationId = r.getIdentifier("slide_right_in", "anim", ctx.getPackageName());
    sSlideOutRightAnimationId = r.getIdentifier("slide_right_out", "anim", ctx.getPackageName());

}