Example usage for android.content Context getApplicationContext

List of usage examples for android.content Context getApplicationContext

Introduction

In this page you can find the example usage for android.content Context getApplicationContext.

Prototype

public abstract Context getApplicationContext();

Source Link

Document

Return the context of the single, global Application object of the current process.

Usage

From source file:Main.java

public static final Application getApplication(Context context) {
    return (Application) context.getApplicationContext();
}

From source file:Main.java

public static String getVersionName(Context context) {
    try {// w w  w.jav a  2s  .  c o m
        return context.getApplicationContext().getPackageManager()
                .getPackageInfo(context.getApplicationContext().getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException ex) {
        return "undefined version name";
    }
}

From source file:Main.java

public static int dip2px(Context context, float dipValue) {
    final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
}

From source file:Main.java

/**
 * Gets the battery info of device, and then writes it to a file.
 * //from   w  w w.  j a  v  a 2  s .  c o  m
 * @param context the context of application
 */
public static String getBatteryUsageInfo(Context context) {
    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra("level", -1);
    double scale = batteryIntent.getIntExtra("scale", -1);
    return String.format("Device has %s of %s battery left", rawlevel, scale);
}

From source file:Main.java

static boolean isPronouncedSound(Context context) {
    Context applicationContext = context.getApplicationContext();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
    return preferences.getBoolean(PLAY_SOUND_ON_SLIDE, true);
}

From source file:Main.java

/**
 * px ---> dp//from ww  w . j a va  2s  . com
 */
public static int px2dp(Context context, float pxValue) {
    final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}

From source file:Main.java

public static int px2sp(Context context, float pxValue) {
    final float fontScale = context.getApplicationContext().getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
}

From source file:Main.java

public static int sp2px(Context context, float spValue) {
    final float fontScale = context.getApplicationContext().getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
}

From source file:Main.java

static File createDefaultCacheDir(Context context) {
    final File cache = new File(context.getApplicationContext().getCacheDir(), AMAZON_CACHE);
    if (!cache.exists()) {
        //noinspection ResultOfMethodCallIgnored
        cache.mkdirs();// ww w  .j a  va  2  s. c  o m
    }
    return cache;
}

From source file:Main.java

/**
 * dp ---> px//from   w  ww. j av a 2  s.co m
 */
public static int dp2px(Context context, float dpValue) {
    final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}