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

@SuppressLint("ShowToast")
public synchronized static Toast getInstance(Context context, int textResId, int duration) {
    if (null == mToast) {
        mToast = Toast.makeText(context.getApplicationContext(), textResId, duration);
    } else {/*from   w  w  w  .  j ava 2s . c o m*/
        mToast.setText(textResId);
        mToast.setDuration(duration);
    }
    return mToast;
}

From source file:Main.java

public static BluetoothManager getBluetoothManager(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return (BluetoothManager) (context.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE));
    }//w  w  w .j  a v  a  2  s .c  o  m
    return null;
}

From source file:Main.java

/** Retrieve a type-face. Does not load twice, uses lazy loading. */
public static Typeface getTypeface(Context context, String name) {
    // ensure the global context is used. just in case.
    context = context.getApplicationContext();
    Log.d(TAG, "name=" + name);
    if (TYPEFACE_CACHE.containsKey(name)) {
        return TYPEFACE_CACHE.get(name);
    }/*from w  ww  . j a  v  a  2s. co  m*/

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);

    if (typeface != null) {
        TYPEFACE_CACHE.put(name, typeface);
    }

    return typeface;
}

From source file:Main.java

public static void init(Context ctx) {
    packageName = ctx.getPackageName();/*  w w w  . j  av a  2 s.  c  o m*/
    resources = ctx.getResources();
    mContext = ctx.getApplicationContext();
}

From source file:Main.java

public static boolean canBeStarted(Context context, Intent intent, boolean checkSignature) {
    final PackageManager manager = context.getApplicationContext().getPackageManager();
    final ResolveInfo info = manager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (info == null) {
        return false;
    }/*from w ww  . j  av  a2s .  c o  m*/
    final ActivityInfo activityInfo = info.activityInfo;
    if (activityInfo == null) {
        return false;
    }
    if (!checkSignature) {
        return true;
    }
    return PackageManager.SIGNATURE_MATCH == manager.checkSignatures(context.getPackageName(),
            activityInfo.packageName);
}

From source file:Main.java

/**
 * show Toast/*  ww w.j  a  v a 2s .c  om*/
 *
 * @param context
 * @param message
 */
public static void showToast(Context context, String message) {
    if (TextUtils.isEmpty(message)) {
        return;
    }
    Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

From source file:Main.java

public static Object get(Context context, String key) {
    Object service = context.getSystemService(key);
    if (service == null) {
        service = context.getApplicationContext().getSystemService(key);
    }//from  w w w . ja v a2s .  co  m
    return service;
}

From source file:Main.java

/**
 * @param context// w  w  w . jav a  2s  . c  om
 * @return the {@link SharedPreferences} for this app
 */
public static SharedPreferences getSharedPreferences(Context context) {
    // Uses the Application Context
    return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
}

From source file:Main.java

public static void hideKeyboard(Context context, View view) {
    if (context != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            if (view != null) {
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }//from  w w w  .  j  a  va2  s  .c  o  m
        }
    }
}

From source file:Main.java

@TargetApi(14)
private static NfcAdapter getAdapter(Context context) {
    if (Build.VERSION.SDK_INT < 14)
        return null;

    return NfcAdapter.getDefaultAdapter(context.getApplicationContext());
}