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 float getDisplayParams(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = context.getApplicationContext().getResources().getDisplayMetrics();
    return dm.density;
}

From source file:Main.java

public static void setHideApplication(Context c, boolean hide) {
    ComponentName cn = new ComponentName(c.getApplicationContext(), ALIAS_CLASSNAME);
    int setting = hide ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
            : PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    int current = c.getPackageManager().getComponentEnabledSetting(cn);
    if (current != setting) {
        c.getPackageManager().setComponentEnabledSetting(cn, setting, PackageManager.DONT_KILL_APP);
    }// w ww  .ja  v  a  2  s  . c om
}

From source file:Main.java

public static void savePreference(Context context, boolean bEnableService) {
    SharedPreferences pref = context.getApplicationContext().getSharedPreferences(PREFERENCE_NAME, PREF_MODE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean(PREF_ENABLE_SERVICE, bEnableService);
    editor.commit();// ww w .  j a  v  a  2s.  co  m

}

From source file:Main.java

public static boolean isNetworkConnected(@NonNull Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

From source file:Main.java

/**
 * Release the devices screen lock./*from ww  w . j  a  v a 2 s  . c  o m*/
 * @param context
 */
public static void releaseScreenLock(Context context) {
    KeyguardManager keyguardManager = (KeyguardManager) context.getApplicationContext()
            .getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();
}

From source file:Main.java

public static float getDensity(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = context.getApplicationContext().getResources().getDisplayMetrics();
    float density = dm.density;
    return density;
}

From source file:Main.java

public static Account getAccount(Context context, String type) {
    Account[] accountsByType = AccountManager.get(context.getApplicationContext()).getAccountsByType(type);
    return (accountsByType == null || accountsByType.length <= 0) ? null : accountsByType[0];
}

From source file:Main.java

private static void bindToService(Intent intent, Context context, ServiceConnection callback) {
    Context realActivity = context.getApplicationContext();
    if (realActivity == null) {
        realActivity = context;/*from w  w w .  j ava  2  s  . co m*/
    }
    ContextWrapper cw = new ContextWrapper(realActivity);
    cw.startService(intent);
    cw.bindService(intent, callback, Context.BIND_AUTO_CREATE);
}

From source file:Main.java

private static PackageManager getPackageManager(Context context) {
    if (packageManager == null) {
        packageManager = context.getApplicationContext().getPackageManager();
    }/* w w w .  java  2  s.c o m*/
    return packageManager;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager manager = (ConnectivityManager) ctx.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }/* w ww  .j a  va  2 s.  c  om*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}