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 getIdResource(Context ctx, String name) {
    String packageName = ctx.getPackageName();
    try {//from www .j  a v  a2 s.  co m
        Object obj;
        obj = Class.forName(packageName + ".R$id").newInstance();
        return Class.forName(packageName + ".R$id").getDeclaredField(name).getInt(obj);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static int getLayoutResource(Context ctx, String name) {
    String packageName = ctx.getPackageName();
    try {//from  ww w. j  av  a 2  s.  co m
        Object obj;
        obj = Class.forName(packageName + ".R$layout").newInstance();
        return Class.forName(packageName + ".R$layout").getDeclaredField(name).getInt(obj);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getPrefFilePath(Context context, String filename) {
    final String packageName = context.getPackageName();
    return "/data/data/" + packageName + "/shared_prefs/" + filename + ".xml";
}

From source file:Main.java

public static void cleanAllSP(Context context) {
    String packageName = context.getPackageName();
    SharedPreferences sp = context.getSharedPreferences(packageName, Context.MODE_PRIVATE);
    Editor editor = sp.edit();/*from   ww w  . j  a  va2 s  .  com*/
    editor.clear();
    editor.apply();
}

From source file:Main.java

public static int getDrawableResource(Context ctx, String name) {
    String packageName = ctx.getPackageName();
    try {/*w  w w .  jav a  2s.  c o  m*/
        Object obj;
        obj = Class.forName(packageName + ".R$drawable").newInstance();
        return Class.forName(packageName + ".R$drawable").getDeclaredField(name).getInt(obj);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getVersionName(Context context) {
    try {/*from  www.j a  va 2  s.  c om*/
        String pkName = context.getPackageName();
        String versionName = context.getPackageManager().getPackageInfo(pkName, 0).versionName;
        return versionName;
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static void openPlayStore(Context context) {
    final String appPackageName = context.getPackageName();
    try {//from  w  ww  . j  a  v  a 2s. c  o  m
        context.startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
    } catch (android.content.ActivityNotFoundException anfe) {
        context.startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
    }
}

From source file:Main.java

public static String getVersionName(Context context) {
    try {// w  ww  . j  a va  2  s .  co  m
        String pkName = context.getPackageName();
        String versionName = context.getPackageManager().getPackageInfo(pkName, 0).versionName;
        return versionName;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String getRawUriString(Context context, int rawResId) {
    return "android.resource://" + context.getPackageName() + "/" + rawResId;
}

From source file:Main.java

public static String getAppPackageName(Context context) {
    Context applicationText = context.getApplicationContext();
    return applicationText.getPackageName();
}