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 isNetworkAvailable(Context context) {
    if (context != null) {
        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }//from   ww  w.  j a va 2  s . c  o  m
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }

        if (networkinfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Retrieves the PackageInfo object for this application.
 *
 * @param context Any context./* ww w  . j  a  va  2 s  .  co m*/
 * @return The PackageInfo object for this application.
 */
public static PackageInfo getOwnPackageInfo(Context context) {
    PackageManager manager = context.getPackageManager();
    try {
        String packageName = context.getApplicationContext().getPackageName();
        return manager.getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        // Should never happen.
        throw new AssertionError("Failed to retrieve own package info");
    }
}

From source file:Main.java

private static File createCacheDir(final Context context, final String dirName) {
    final File cache = new File(context.getApplicationContext().getCacheDir(), dirName);
    if (!cache.exists()) {
        cache.mkdirs();/*from w w  w.ja v a 2s. c o m*/
    }
    return cache;
}

From source file:Main.java

public static SharedPreferences getDefaultSharedPreferences(Context _context) {
    if (sPreferences == null) {
        sPreferences = PreferenceManager.getDefaultSharedPreferences(_context.getApplicationContext());
    }// w w w .ja  v a 2  s . c o  m
    return sPreferences;
}

From source file:Main.java

protected static SharedPreferences createPrefs(Context context, String name, int mode) {
    if (TextUtils.isEmpty(name)) {
        return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    } else {//from w  w w.  ja va2 s .c  o m
        return context.getSharedPreferences(name, mode);
    }
}

From source file:Main.java

public static int getScreenWidth(Context context) {
    if (null == context) {
        return 0;
    }//from  w ww .j  a v a 2s  .  c  o  m
    DisplayMetrics dm = new DisplayMetrics();
    dm = context.getApplicationContext().getResources().getDisplayMetrics();
    return dm.widthPixels;
}

From source file:Main.java

/**
 * @return the absolute path to the AnkiDroid directory.
 *///  w w w .j  a  va2 s.  c o m
public static String getCurrentAnkiDroidDirectory(Context context) {
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(context.getApplicationContext());
    return preferences.getString("deckPath", getDefaultAnkiDroidDirectory());
}

From source file:Main.java

public static void reloadLocale(Context context) {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context.getApplicationContext());
    String lang = sharedPrefs.getString(PREF_APP_LANGUAGE, Locale.getDefault().getLanguage());
    if (sharedPrefs.getString(PREF_APP_LANGUAGE, "").equalsIgnoreCase("")) {
        sharedPrefs.edit().putString(PREF_APP_LANGUAGE, lang).commit();
    }//from  w  ww.j a  va2s .  c o m
    Configuration newConfig = context.getResources().getConfiguration();

    if (!newConfig.locale.getLanguage().equals(lang)) {
        locale = new Locale(lang);
    }

    if (locale != null) {
        Locale.setDefault(locale);
        newConfig.locale = locale;
        context.getResources().updateConfiguration(newConfig, context.getResources().getDisplayMetrics());
    }
}

From source file:Main.java

@SuppressWarnings("ResourceType")
@SuppressLint("ShowToast")
public static void showToastMessage(Context context, String msg) {

    try {/*  w  ww.  java2  s .  c  o m*/
        if (msg != null && !msg.equalsIgnoreCase("")) {
            Toast.makeText(context.getApplicationContext(), msg, 4000).show();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Checks if an App is Installed.//w w w. j  a v a  2 s  . com
 *
 * @return true if installed false otherwise
 */
public static boolean isAppInstalled(Context context, String packageName) {

    if (context == null)
        return false;

    PackageManager pm = context.getApplicationContext().getPackageManager();
    boolean isAppInstalled = false;
    try {

        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        isAppInstalled = true;

    } catch (PackageManager.NameNotFoundException e) {
        isAppInstalled = false;
    }

    return isAppInstalled;
}