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 isInstalledOnSDCard(Context context) {
    return isInstalledOnSDCard((Application) context.getApplicationContext());
}

From source file:Main.java

public static void deleteSharedPreferences(Context context) {
    try {// ww  w  . j a v  a  2 s .  c o m
        Context appContext = context.getApplicationContext();
        ApplicationInfo info = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(),
                0);
        String dirPath = info.dataDir + File.separator + "shared_prefs" + File.separator;
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            String[] list = dir.list();
            int size = list.length;
            for (int i = 0; i < size; i++) {
                new File(dirPath + list[i]).delete();
            }
        } else {
            //Log.d("AAA", "NO FILE or NOT DIR");
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveRes(Context gContext, int res, String dirjpg) {
    InputStream is = gContext.getApplicationContext().getResources().openRawResource(res);
    boolean rez = false;
    try {//from ww w .j a  va  2s  .c o m
        FileOutputStream rtFOS = new FileOutputStream(dirjpg + "/" + res + ".gif");
        byte[] buffer = new byte[4096];
        int n = -1;
        while ((n = is.read(buffer, 0, 4095)) != -1) {
            if (n > 0)
                rtFOS.write(buffer, 0, n);
            rez = true;
        }
        rtFOS.flush();
        rtFOS.close();
    } catch (Exception e) {
        Log.e("FullscreenActivity", e.toString());
    }
    return rez;
}

From source file:Main.java

/**
 * Returns whether location services are currently active in the specified context. Location
 * services are active if both GPS and network location services are enabled.
 * @param context the context from which to check
 * @return true if there location services are on, otherwise false
 *///from   w  w  w . j  a  v a  2 s .c o  m
public static boolean isLocationServicesOn(Context context) {
    LocationManager lm = (LocationManager) context.getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = false;
    boolean networkEnabled = false;

    // check whether GPS and network providers are enabled
    try {
        gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {
    }

    try {
        networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception e) {
    }

    // only show dialog if location services are not enabled
    return (gpsEnabled || networkEnabled);
}

From source file:Main.java

public static boolean detect(Context context) {

    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        return false;
    }/*from w w w  .ja  v  a2  s.com*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();

    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }

    return true;
}

From source file:Main.java

public static File getOutputMediaFile(int type, Context c) {
    File mediaStorageDir = new File(c.getApplicationContext().getExternalFilesDir(null).getAbsolutePath());
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }/*from w  w  w .ja v  a2 s .c o  m*/
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_AUDIO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".3gp");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

public static String getPackageName(Context context) {
    if (context == null) {
        return null;
    }//from w w w.jav a2 s  .c o m
    return context.getApplicationContext().getPackageName();
}

From source file:Main.java

public static boolean isAppOnForeground(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx.getApplicationContext()//
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = ctx.getApplicationContext().getPackageName();

    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }/*from   w  w w . ja  v  a  2  s. c  om*/

    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void showToast(CharSequence message, Context context) {
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context.getApplicationContext(), message, duration);
    toast.show();/*ww w.  j a  va2 s  .c om*/
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        return false;
    }/*from  ww  w .  j av  a  2 s.co  m*/

    NetworkInfo networkinfo = manager.getActiveNetworkInfo();

    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }

    return true;
}