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 isNetConnect(@NonNull Context context) {
    ConnectivityManager service = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = service.getActiveNetworkInfo();

    return info != null && info.isAvailable();
}

From source file:Main.java

public static boolean isOnline(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

From source file:Main.java

public static boolean CheckNet(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }/*from  www  .  j a  v a  2s .co m*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static int dip2px(Context cx, float spValue) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = cx.getApplicationContext().getResources().getDisplayMetrics();
    return (int) (spValue * dm.density + 0.5f);
}

From source file:Main.java

public static int sp2px(Context cx, float spValue) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = cx.getApplicationContext().getResources().getDisplayMetrics();
    return (int) (spValue * dm.scaledDensity + 0.5f);
}

From source file:Main.java

public static Boolean isNetworkConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }//  w ww . j ava  2  s.  c o  m
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    try {// w  w w  .java 2s .co m
        PackageInfo packageInfo = context.getApplicationContext().getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return 1;
}

From source file:Main.java

public static File getOutputMediaFile(int type, Context c) {
    File mediaStorageDir = new File(c.getApplicationContext().getExternalFilesDir(null).getAbsolutePath());
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }//from  w  ww .  java2s . 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 int getVersionCode(Context cx) {
    int versionCode = -1;
    try {//ww w  .  j av a2  s  . com
        versionCode = cx.getApplicationContext().getPackageManager().getPackageInfo(cx.getPackageName(),
                0).versionCode;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
    }

    return versionCode;
}

From source file:Main.java

public static boolean hasNetwork(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }//from   w w  w. j a v  a  2  s  .c  o  m
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;

}