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 isAppOnBackground(Context context) {
    // Returns a list of application processes that are running on the
    // device// ww  w.j a va 2s .c o  m

    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getApplicationContext().getPackageName();

    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;

    for (RunningAppProcessInfo appProcess : appProcesses) {
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnForeground(Context context) {
    // Returns a list of application processes that are running on the
    // device/*  ww w  .j  ava  2  s .c o m*/

    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getApplicationContext().getPackageName();

    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;

    for (RunningAppProcessInfo appProcess : appProcesses) {
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

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(),
                    "fonts/" + font + ".ttf");
            sTypefaceCache.put(font, tf);
        }/*  www.j  av  a  2 s  .c o  m*/
        return sTypefaceCache.get(font);
    }
}

From source file:Main.java

/**
 * Gets the default NFC adapter.//from ww  w . j av a2 s  .c  om
 *
 * @param context the {@link Context}.
 * @return the NFC adapter; or null if the device does not support NFC.
 */
private static NfcAdapter getNfcAdapter(Context context) {
    NfcAdapter nfcAdapter = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        nfcAdapter = NfcAdapter.getDefaultAdapter(context.getApplicationContext());
    }

    return nfcAdapter;
}

From source file:Main.java

public static String getSdCacheDir(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        if (true) {
            Context appContext = context.getApplicationContext();

            File amap = appContext.getExternalFilesDir("offlineMap");
            return amap.getAbsolutePath() + "/";
        } else {/*from w w  w .j  a  v a  2 s. c  o m*/

            File fExternalStorageDirectory = Environment.getExternalStorageDirectory();
            File autonaviDir = new File(fExternalStorageDirectory, "amapsdk");
            boolean result = false;
            if (!autonaviDir.exists()) {
                result = autonaviDir.mkdir();
            }
            File minimapDir = new File(autonaviDir, "offlineMap");
            if (!minimapDir.exists()) {
                result = minimapDir.mkdir();
            }
            return minimapDir.toString() + "/";
        }

    } else {
        return "";
    }
}

From source file:at.florian_lentsch.expirysync.NotifyChecker.java

/**
 * Sets the alarm that checks for products soon to expire (or already have
 * expired)/*from  w w w.j  a  v  a  2 s.c  o m*/
 * 
 * @param context
 */
protected static void setAlarm(Context context) {
    Context appContext = context.getApplicationContext();
    Intent receiverIntent = new Intent(appContext, NotifyChecker.class);

    // Fetch info about when the alarm is to sound each day from the shared
    // preferences:
    long firstStartMillis = getFirstStartMillis(appContext);
    if (firstStartMillis < 0) {
        Log.i("Alarm", "Alert time not configured - not setting alarm");
        return;
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(firstStartMillis);
    // Log.i("Alarm", "Setting alarm: " + firstStartMillis + ", " + (new
    // SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
    // Locale.US)).format(firstStartMillis));

    // Set the alarm:
    PendingIntent alarmIntent = PendingIntent.getBroadcast(appContext, 0, receiverIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmMgr = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, firstStartMillis, AlarmManager.INTERVAL_DAY, alarmIntent);
}

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

public static SegmentData fetchSegment(Context c, long segment_id) {
    SegmentData t = new SegmentData(c.getApplicationContext(), segment_id);
    t.populateDetails();/* ww  w  .ja va2 s  . com*/
    return t;
}

From source file:Main.java

public static String getHomeScheme(Context ctx) {
    if (TextUtils.isEmpty(homeScheme)) {
        homeScheme = getMetaData(ctx.getApplicationContext(), "MAIN_SCHEME");
    }//from   www.ja v a2s  .  c  om

    return homeScheme;
}

From source file:Main.java

/**
 * get a unique hash of the device.//from  ww w .  j a va2s. c om
 * 
 * @return a unique hash of the device.
 */
public static String getDeviceId(Context ctx) {
    if (ctx == null) {
        return null;
    }

    String androidId = "" + android.provider.Settings.Secure.getString(
            ctx.getApplicationContext().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    String deviceName = getDeviceName();

    UUID deviceUuid = new UUID(androidId.hashCode(),
            (long) deviceName.hashCode() << 32 | deviceName.hashCode());
    String deviceId = deviceUuid.toString().replace("-", "");

    return deviceId;
}

From source file:Main.java

public static void sendBroadcast(Context context, Intent intent) {
    if (mLocalBroadcastManager == null) {
        getLocalBroadcastManager(context.getApplicationContext());
    }/*from   w w w .j ava  2s  .c o  m*/
    mLocalBroadcastManager.sendBroadcast(intent);
}