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 void saveStartTime(Context paramContext) {
    SharedPreferences time = paramContext
            .getSharedPreferences("mobclick_agent_" + paramContext.getPackageName(), 0);
    Editor editor = time.edit().putLong("start_millis", System.currentTimeMillis());
    editor.commit();/*from   w  w  w  .ja  v a 2  s .  co  m*/
}

From source file:Main.java

public static final String getAppPackageName(Context context) {
    String ret = null;/* w w w  . j  a  va2 s.  co  m*/
    try {
        ret = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_ACTIVITIES).packageName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return ret;
}

From source file:Main.java

/**
 * Get meta data of named node: fix bug for integer values
 *//* ww  w  .  j  a v  a2 s .com*/
public static String getAppMetaData(Context context, String name) {
    try {
        return context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).metaData.get(name).toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static boolean checkPermission(Context context, String permission) throws NullPointerException {
    return context.getPackageManager().checkPermission(permission, context.getPackageName()) != 0;
}

From source file:Main.java

public static String getAppVersion(Context context) {
    try {// w  w  w  . j  a va2  s .c  o m
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        LOGE("Package name not found: " + e.getMessage());
    }
    return "(uknown version)";
}

From source file:Main.java

@SuppressLint("NewApi")
public static boolean isDefaultSmsProvider(Context context) {
    return (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
            || (context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context)));
}

From source file:Main.java

public static String getPackageName(Context context) {
    try {//from  ww  w  .ja  v  a2s .c o m
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return info.packageName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void printHashKey(Context context) {
    try {/*from www .j  ava2 s  .co m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

public static String getVerName(Context context) {
    String verName = "";
    try {/*w  w  w  .  j a v  a 2  s.c  o m*/
        verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return verName;
}

From source file:Main.java

public static String getDataDirectory(Context context) throws IOException {
    try {//from  ww w .  j a va2 s . c  om
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
    } catch (PackageManager.NameNotFoundException nnfe) {
        throw new IOException("Cannot access to data directory", nnfe);
    }
}