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

static public int getColor(Context context, String name) {
    int identifier = context.getResources().getIdentifier(name, "color", context.getPackageName());
    if (identifier > 0) {
        return context.getResources().getColor(identifier);
    }/*from  ww  w .  ja  va2  s.c  o m*/
    return Color.TRANSPARENT;
}

From source file:Main.java

public static int getResStr(Context paramContext, String paramString) {
    return paramContext.getResources().getIdentifier(paramString, "string", paramContext.getPackageName());
}

From source file:Main.java

/**
 * Returns the key hashes of the signatures of the calling app which may be needed when
 * integrating with 3rd party services such as Facebook. Note that Android apps usually only
 * have one signature./*from  www . j a va 2 s .  c  o m*/
 *
 * @param context
 * @return The key hashes
 */
public static List<String> getKeyHashes(Context context) {
    return getKeyHashes(context, context.getPackageName());
}

From source file:Main.java

public static void clearDefaultOpenSetting(Context mContext) {
    PackageManager pm = mContext.getPackageManager();
    pm.clearPackagePreferredActivities(mContext.getPackageName());
}

From source file:Main.java

public static String getAppMetaData(Context context, String key) {
    try {/* ww w  .  j av a  2  s .  c  om*/
        return context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).metaData.getString(key);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getVersionName(Context context) {
    try {/* w  ww .  jav  a2s.  c o m*/
        String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).versionName;
        return versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static int getResId(Context ctx, String resourceName) {
    return ctx.getResources().getIdentifier(resourceName, "drawable", ctx.getPackageName());
}

From source file:Main.java

/**
 * Gets the resource id from the name and type
 * ex... getResIdFromIdentifier(ctx, "error_msg", "string");
 * @param context/*from w w w . j a  v  a  2s  .co m*/
 * @param name
 * @param defType
 * @return resourceId
 */
public static int getResIdFromIdentifier(Context context, String name, String defType) {
    return context.getResources().getIdentifier(name, defType, context.getPackageName());
}

From source file:Main.java

/*********************************************************************************
 * Returns the resource-IDs for all attributes specified in the given
 * <declare-styleable>-resource tag as an int array.
 *
 * @param context//  w  ww . ja v a 2 s.c o m
 *            The current application context.
 * @param name
 *            The name of the <declare-styleable>-resource-tag to pick.
 * @return All resource-IDs of the child-attributes for the given
 *         <declare-styleable>-resource or <code>null</code> if this tag
 *         could not be found or an error occured.
 *********************************************************************************/
public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) {
    try {
        Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
        for (Field f : fields2) {
            if (f.getName().equals(name)) {
                int[] ret = (int[]) f.get(null);
                return ret;
            }
        }
    } catch (Throwable t) {
    }

    return null;
}

From source file:Main.java

public static int getAppIconResId(Context app) {
    PackageManager pm = app.getPackageManager();
    String packageName = app.getPackageName();
    try {//  w w w.ja v a  2  s  .  c o m
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return ai.icon;
    } catch (Exception e) {
        e.printStackTrace();
        try {
            return app.getResources().getIdentifier("sym_def_app_icon", "mipmap", "android");
        } catch (Exception e1) {
            e1.printStackTrace();
            return 0;
        }
    }
}