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 getAppPackageName(Context context) {
    String pkName = null;/*ww w  . jav  a  2s .  c o m*/
    try {
        pkName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return pkName;
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    String verCode = null;//from www.  j  a  v a 2 s . co  m
    try {
        verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
    }
    return verCode;
}

From source file:Main.java

public static int getMetaInteger(Context context, String key) throws Exception {
    ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
            PackageManager.GET_META_DATA);
    return Integer.parseInt(applicationInfo.metaData.get(key).toString());
}

From source file:Main.java

public static int getVersionCode(Context context) {
    int verCode = -1;
    try {//from   w  w w.j  ava  2 s  .  co m
        verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return verCode;
}

From source file:Main.java

public static int getIdByName(Context context, String className, String name) {
    String packageName = context.getPackageName();
    Class r = null;/*from  w  w  w .  j a v  a  2  s . c  o  m*/
    int id = 0;
    try {
        r = Class.forName(packageName + ".R");

        Class[] classes = r.getClasses();
        Class desireClass = null;

        for (int i = 0; i < classes.length; ++i) {
            if (classes[i].getName().split("\\$")[1].equals(className)) {
                desireClass = classes[i];
                break;
            }
        }
        if (classes.length == 0) {
            desireClass = Class.forName(packageName + ".R$" + className);
        }
        if (desireClass != null)
            id = desireClass.getField(name).getInt(desireClass);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return id;
}

From source file:Main.java

public static int getVersionCode(Context context) {
    int versionCode = 0;
    try {// w  ww.  j a v a  2 s .com
        versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (Exception e) {
    }
    return versionCode;
}

From source file:Main.java

public static int getVersionCode(Context context) {
    int versionCode = 0;
    try {// w  w w .j a  v  a2 s .c  om
        versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException ignored) {
    }
    return versionCode;
}

From source file:Main.java

public static int getStatus(Context context) {
    Cursor query = null;/*from   ww w  . j av a  2s .  c om*/
    String packageName = context.getPackageName();
    try {
        query = context.getContentResolver().query(Uri.parse("content://com.lbe.security.miui.permmgr/active"),
                null, "pkgName=?", new String[] { packageName }, null);
        if (query == null) {
            return 0;
        }
        if (query.moveToFirst()) {
            int status = query.getInt(query.getColumnIndex("userAccept"));
            if (query == null) {
                return status;
            }
        }
        query.close();
    } catch (Exception e) {

        return -1;
    }

    return -1;
}

From source file:Main.java

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

From source file:Main.java

public static int getResourseIdByName(Context context, String resourseType, String resourceName) {

    return context.getResources().getIdentifier(resourceName, resourseType, context.getPackageName());
}