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

/**
 * get app package info/*w w w  . ja  v a2 s.co m*/
 */
public static PackageInfo getAppPackageInfo(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        return pm.getPackageInfo(context.getPackageName(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getversion(Context context) {
    PackageInfo pInfo = null;/*ww w . jav  a  2s . c o m*/
    try {
        pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return pInfo == null ? "" : "V " + pInfo.versionName;
}

From source file:Main.java

public static String getPackageName(Context context) {
    PackageInfo info = null;//w  w  w .  j a v a 2 s .c  om
    try {
        info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    return info.packageName;
}

From source file:Main.java

public static boolean isMainProcess(Context context) {
    // TODO if mainProcess is not packageName, you should modify here
    String mainProcess = context.getPackageName();
    return TextUtils.equals(getProcessName(context, android.os.Process.myPid()), mainProcess);
}

From source file:Main.java

public static String getCurrentVersion(Context context) {
    PackageInfo pInfo = null;/*from www . j a v  a 2s .  c  om*/
    try {
        pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    return "v" + pInfo.versionName;
}

From source file:Main.java

public static int getResourceId(Context context, String type, String name) {
    try {//from w ww. ja va  2s  . c o m
        int resID = context.getResources().getIdentifier(name, type, context.getPackageName());
        return resID;
    } catch (Exception e) {
        new StringBuilder("uiType: ").append(type).append(" idName: ").append(name).toString();
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static int getVersionCode(Context context) {
    PackageInfo pInfo = null;//from   www.  j a  v a  2  s .  co m
    try {
        pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        //practically cant happen
    }
    return pInfo.versionCode;
}

From source file:Main.java

public static String getVersionName(Context context) {
    String versionName;//w  ww  .  j  ava 2 s  .com

    try {
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = "Unknown";
    }

    return versionName;
}

From source file:Main.java

static public int getAccentColor(Context context) {
    int identifier = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
    if (identifier == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        identifier = android.R.attr.colorAccent;
    }/*  w w w  .  j a  v  a  2 s  . c  o  m*/
    if (identifier > 0) {
        TypedValue typedValue = new TypedValue();
        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] { identifier });
        if (a != null) {
            int color = a.getColor(0, 0);
            a.recycle();
            return color;
        }
    }
    return Color.BLACK;
}

From source file:Main.java

public static String getMetaData(Context context, String key) {
    try {/*from  w  ww.  ja  v  a 2 s  .  co m*/
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);
        Object value = ai.metaData.get(key);
        if (value != null) {
            return value.toString();
        }
    } catch (Exception ignored) {
    }
    return null;
}