/** * 网络是否手机网络连接 * * @param context * @return */ public static boolean isOnlyMobileType(Context context) { State wifiState = null; State mobileState = null; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = null; try { networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); } catch (Exception e) { e.printStackTrace(); } if (networkInfo != null) { wifiState = networkInfo.getState(); } try { networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); } catch (Exception e) { e.printStackTrace(); } if (networkInfo != null) { mobileState = networkInfo.getState(); } LogS.d("zhang", "onReceive -- wifiState = " + wifiState + " -- mobileState = " + mobileState); if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) { // 手机网络连接成功 LogS.d("zhang", "onReceive -- 手机网络连接成功"); return true; } return false; }
/** * 获取文件系统的剩余空间,单位:KB * * @return */ public static long getFileSystemAvailableSize(File dirName) { long availableSize = -1; if (dirName != null && dirName.exists()) { StatFs sf = new StatFs(dirName.getPath()); long blockSize = sf.getBlockSize(); long blockCount = sf.getBlockCount(); long availableBlocks = sf.getAvailableBlocks(); availableSize = availableBlocks * blockSize / 1024; LogS.d( TAG, "blockSize = " + blockSize + ", blockCount = " + blockCount + ", totalSize = " + blockSize * blockCount / 1024 + " KB" + "\navailableBlocks = " + availableBlocks + ", availableSize = " + availableSize + " KB"); } return availableSize; }
/** * sdcard是否可读写 * * @return */ public static boolean isSdcardReady() { try { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } catch (Exception e) { LogS.e(TAG, "isSdcardReady had exception!", e); return false; } }
/** * 程序是否在前台运行 * * @return */ public static boolean isAppOnForeground(Context context) { try { ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); String packageName = context.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; } } } catch (Exception e) { e.printStackTrace(); LogS.e(TAG, "isAppOnForeground exception!", e); } return false; }