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 int getAppVersionCode(Context context) {
    int version = 0;
    try {/*from   ww w. j av a 2  s .c  om*/
        version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("the application not found");
    }
    return version;
}

From source file:Main.java

public static int getVersionCode(Context context) {

    int versionCode = 0;
    try {//from w w  w  .j  av  a2s.c  o m
        versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return versionCode;
}

From source file:Main.java

public static int getDrawableResId(Context context, String name) {
    return context.getResources().getIdentifier(name, android.R.drawable.class.getSimpleName(),
            context.getPackageName());
}

From source file:Main.java

/**
 * Calls {@link Context#startActivity(Intent)} with the given {@link Intent}. If it is
 * <b>implicit</b>, makes sure there is an Activity to handle it. If <b>explicit</b>,
 * will intercept {@link android.content.ActivityNotFoundException}. Can show an error toast on
 * failure.//from  www .j a v  a  2s  .c o  m
 *
 * <p> E.g. an implicit intent may fail if e.g. the web browser has been disabled through
 * restricted profiles.
 *
 * @return Whether the {@link Intent} could be handled.
 */

public static String getVersion(Context context) {
    String version;
    try {
        version = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        version = "UnknownVersion";
    }
    return version;
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    String versionName = "";
    try {/*w  ww.ja v  a 2 s. co  m*/
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        if ((versionName != null) && (versionName.length() > 0))
            return versionName;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return versionName;
}

From source file:Main.java

public static String getVersionName(Context app) {
    PackageInfo packageInfo = null;//from  w w  w . ja v  a 2s.c o  m
    try {
        packageInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static Intent getIntent(Context paramContext) {
    StringBuilder localStringBuilder = new StringBuilder().append("market://details?id=");
    String str = paramContext.getPackageName();
    localStringBuilder.append(str);/*from   w w  w .  ja v a  2  s .  co m*/
    Uri localUri = Uri.parse(localStringBuilder.toString());
    return new Intent("android.intent.action.VIEW", localUri);
}

From source file:Main.java

public static void openMyAppInfo(Context mContext) {
    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    Uri uri = Uri.fromParts(SCHEME, mContext.getPackageName(), null);
    intent.setData(uri);//w w  w  . ja  va  2 s .c  om
    mContext.startActivity(intent);
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    int version = 0;
    try {/*from ww  w . j  a v a  2s  .co m*/
        version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

public static String getAppVersionName(Context context) {
    PackageManager pm = context.getPackageManager();
    try {/*from  w  w w .  j  a  v a  2 s  . c o  m*/
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return pi == null ? "" : pi.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}