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

/**
 * A forced setting of an application context for the SDK.
 * That method must be call in services of broadcast events for prepare the SDK for a work.
 *
 * @param appContext Context of application
 *///from   w  ww . j  a  v  a  2  s . c o  m
static void setApplicationContext(Context appContext) {
    if (appContext != null) {
        sApplicationContext = appContext.getApplicationContext();
    }
}

From source file:Main.java

public static void showLong(Context context, @StringRes int resId) {
    if (context != null) {
        Toast.makeText(context.getApplicationContext(), resId, Toast.LENGTH_LONG).show();
    }/*from   w w  w .ja  v a2s.c  o  m*/
}

From source file:Main.java

/**
 * Gets the calling package names for the current transaction.
 * @param context The context to use for accessing the package manager.
 * @return The calling package names./*from w ww. j  a v  a2  s  . co  m*/
 */
private static String[] getCallingPackages(Context context) {
    int callingUid = Binder.getCallingUid();
    PackageManager pm = context.getApplicationContext().getPackageManager();
    return pm.getPackagesForUid(callingUid);
}

From source file:Main.java

public static void showShort(Context context, @StringRes int resId) {
    if (context != null) {
        Toast.makeText(context.getApplicationContext(), resId, Toast.LENGTH_SHORT).show();
    }//from w  ww . j  a  va  2  s  .  co m
}

From source file:Main.java

private static synchronized void createToast(Context context) {
    if (sToast == null) {
        sToast = Toast.makeText(context.getApplicationContext(), "", Toast.LENGTH_SHORT);
    }/* w w w. j  a v  a  2 s. c  om*/
}

From source file:Main.java

public static String getApplicationName(Context ctx) {
    ApplicationInfo ai;/*from www.j  av  a  2 s  . co m*/
    try {
        Context appContext = ctx.getApplicationContext();
        assert appContext != null;
        final PackageManager pm = appContext.getPackageManager();
        assert pm != null;
        ai = pm.getApplicationInfo(ctx.getPackageName(), 0);
        return (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    } catch (Exception ignored) {
    }
    return null;
}

From source file:Main.java

public static int getDisplayWidth(Context context) {
    if (displayWidth <= 0) {
        WindowManager wm = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        displayWidth = dm.widthPixels;// w w  w  .j  a v a2 s .c  o m
    }
    return displayWidth;
}

From source file:Main.java

public static int getDisplayHeight(Context context) {
    if (displayHeight <= 0) {
        WindowManager wm = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        displayHeight = dm.heightPixels;
    }/*from   w  w w  . ja va 2 s  .c  om*/
    return displayHeight;
}

From source file:Main.java

public static PackageInfo getOtherPackageInfo(Context context, String packageName) {
    PackageInfo info = null;//from  ww  w . java2s .  com
    try {
        info = context.getApplicationContext().getPackageManager().getPackageInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
    return info;
}

From source file:Main.java

/**
 * DisplayMetrics/*w  ww.j a va2  s .c  o m*/
 *
 * @return
 */
public static DisplayMetrics getDisplayMetrics(Context context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics;
}