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 boolean isInstall(Context context, String packagename) {
    boolean isInstall = false;
    PackageInfo packageInfo;/*from  w  w  w  .ja  v a2 s  .co m*/

    try {
        packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(packagename, 0);

    } catch (NameNotFoundException e) {
        packageInfo = null;
        e.printStackTrace();
    }
    if (packageInfo == null) {
        isInstall = false;
    } else {
        isInstall = true;
    }
    return isInstall;
}

From source file:Main.java

/**
 * @return true is device is plugged at this moment, false otherwise.
 * @see #isPlugged(android.content.Intent)
 *///from   ww w  .j  a  v a2s  .c  o m
public static boolean isPlugged(@NonNull Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    return isPlugged(context.getApplicationContext().registerReceiver(null, intentFilter));
}

From source file:Main.java

public static final boolean isKeyguardWidget(int appWidgetId, Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());

        return appWidgetManager.getAppWidgetOptions(appWidgetId).getInt(
                AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
                -1) == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
    }/* w ww  . j  a va2  s  .c  om*/

    return false;
}

From source file:Main.java

public static float getWidthDpi(Context context) {
    DisplayMetrics dm = null;/*from  w  w w  .  j a  v  a2s.  c o m*/
    try {
        if (context != null) {
            dm = new DisplayMetrics();
            dm = context.getApplicationContext().getResources().getDisplayMetrics();
        }

        return dm.densityDpi;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;

}

From source file:Main.java

private static ConnectivityManager getConnectivityManager(Context context) {
    // use application context to avoid leaking any activity
    // https://code.google.com/p/android/issues/detail?id=198852
    return (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
}

From source file:Main.java

public static ApplicationInfo getApplicationInfo(Context context, String processName) {
    if (processName == null) {
        return null;
    }//w  w w  .  j ava2 s  . co m

    PackageManager packageManager = context.getApplicationContext().getPackageManager();
    List<ApplicationInfo> appList = packageManager
            .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (ApplicationInfo appInfo : appList) {
        if (processName.equals(appInfo.processName)) {
            return appInfo;
        }
    }
    return null;
}

From source file:Main.java

public static Typeface get(Context context, String font) {
    synchronized (sTypefaceCache) {
        if (!sTypefaceCache.containsKey(font)) {
            Typeface tf = Typeface.createFromAsset(context.getApplicationContext().getAssets(), font + ".ttf");
            sTypefaceCache.put(font, tf);
        }//from  www.j  av a 2  s  . co  m
        return sTypefaceCache.get(font);
    }
}

From source file:foodcenter.android.service.AndroidRequestUtils.java

/**
 * Helper method to get a SharedPreferences instance.
 */// w ww.jav a 2 s .  co  m
public static SharedPreferences getSharedPreferences(Context context) {
    return context.getApplicationContext().getSharedPreferences(SHARED_PREFS, 0);
}

From source file:edu.pdx.cecs.orcycle.SegmentData.java

public static SegmentData createSegment(Context c) {
    SegmentData t = new SegmentData(c.getApplicationContext(), 0);
    t.createSegmentInDatabase(c);//from w w  w.j  a  v a 2s .  co  m
    t.initializeData();
    return t;
}

From source file:Main.java

/**
 * @param context/*from w w w .j  a  v  a2 s .  co m*/
 * @return boolean
 * @throws
 * @Title: currentNetworkIsWifi
 * @Description: TODO
 */
public static boolean currentNetworkIsWifi(Context context) {
    if (checkPermission(context, "android.permission.ACCESS_WIFI_STATE"))
        ;
    Context contextApplication = context.getApplicationContext();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getTypeName().equals("WIFI") && info[i].isConnected())
                    return true;
            }
        }
    }
    return false;
}