package com.hz2hs.utils; import java.io.File; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import com.hz2hs.constant.Constant; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; import android.net.TrafficStats; import android.os.Environment; import android.os.StatFs; public class Util { /** * * getCurrentTime:获取系统当前时间 * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-3 上午11:30:05 * @param @return * @return String * @throws */ public static String getCurrentTime() { SimpleDateFormat sDateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); return sDateFormat.format(new java.util.Date()); } /** * * px2dip:px转dp * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-3 上午11:30:26 * @param @param context * @param @param pxValue * @param @return * @return int * @throws */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * * dip2px:dp转px * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-3 上午11:30:41 * @param @param context * @param @param dpValue * @param @return * @return int * @throws */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * * getChineseName:获得SharedPreferences中的chineseame * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-9 上午11:20:57 * @param @param context * @param @return * @return String * @throws */ public static String getChineseName(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("chineseame", ""); } /** * * getUserId:获得SharedPreferences中的userId * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-9 上午11:21:30 * @param @param context * @param @return * @return String * @throws */ public static String getUserId(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("userId", ""); } /** * * getSmId:获得SharedPreferences中的smId * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-9 上午11:21:44 * @param @param context * @param @return * @return String * @throws */ public static String getSmId(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("smId", ""); } /** * * getPhoneNum:获得SharedPreferences中的PhoneNum * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 下午2:05:55 * @param @param context * @param @return * @return String * @throws */ public static String getPhoneNum(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("account", ""); } /** * * getPassWord:获得SharedPreferences中的password * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 下午1:54:26 * @param @param context * @param @return * @return String * @throws */ public static String getPassWord(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("password", ""); } /** * * getImei:获得SharedPreferences中的imei * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 下午2:07:05 * @param @param context * @param @return * @return String * @throws */ public static String getImei(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("imei", ""); } /** * * getImsi:获得SharedPreferences中的imsi * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 下午2:07:54 * @param @param context * @param @return * @return String * @throws */ public static String getImsi(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("imsi", ""); } /** * * getAgentId:获得SharedPreferences中的agentId * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-12 下午3:07:24 * @param @param context * @param @return * @return String * @throws */ public static String getAgentId(Context context) { SharedPreferences userInfo = context.getSharedPreferences("user_info", 0); return userInfo.getString("agentId", ""); } /** * 检查SD卡状态,如果sd卡不存在或者SD卡剩余空间不足禁止程序启动 * * @return */ public static boolean checkSDCardState() { String state = Environment.getExternalStorageState(); File sd = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(sd.getPath()); double blockSize = stat.getBlockSize(); double freeBlocks = stat.getAvailableBlocks(); double countSize = blockSize * freeBlocks; long role = 1024 * 1024 * 10; if (state.equals(Environment.MEDIA_MOUNTED) && countSize > role) { return true; } return false; } /** * * isConnectNet:是否连接到网络 * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-2-17 下午3:45:47 * @param @param context * @param @return * @return Boolean * @throws */ public static Boolean isConnectNet(Context context) { ConnectivityManager con = (ConnectivityManager) context .getSystemService(Activity.CONNECTIVITY_SERVICE); NetworkInfo n1 =con.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo n2 =con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); State wifi = null; State internet = null; if(n1 !=null){ wifi = n1.getState(); } if(n2 !=null){ internet = n2.getState(); } if(internet == State.CONNECTED || internet==State.CONNECTING){ return true; } if(wifi == State.CONNECTED||wifi==State.CONNECTING){ return true; } else { return false; } } /** * * getAppFlow:获取本程序流量统计 * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 上午10:19:26 * @param @return * @return long * @throws */ public static String getAppFlow() { // 获取网络流量 long recv = TrafficStats.getUidRxBytes(android.os.Process.myUid()); long sent = TrafficStats.getUidTxBytes(android.os.Process.myUid()); return sizeToSize(recv + sent + ""); } /** * * sizeToSize:字符串转化成文件大小格式 * * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-4-19 下午1:19:45 * @param @param size * @param @return * @return String * @throws */ public static String sizeToSize(String size) { double i = Double.parseDouble(size); if (i >= 1024 * 1024 && i < 1024 * 1024 * 1024) { i /= 1024 * 1024; DecimalFormat df = new DecimalFormat("#.##"); return (df.format(i) + "MB"); } else if (i >= 1024 * 1024 * 1024) { i /= 1024 * 1024 * 1024; DecimalFormat df = new DecimalFormat("#.##"); return (df.format(i) + "GB"); } else if (i > 0 && i <= 1024) { i /= 1024; DecimalFormat df = new DecimalFormat("#.##"); return (df.format(i) + " B"); } else if (i >= 1024 && i < 1024 * 1024) { i /= 1024 * 1024; DecimalFormat df = new DecimalFormat("#.##"); return (df.format(i) + "KB"); } else { return null; } } private static final double EARTH_RADIUS = 6378137; private static double rad(double d) { return d * Math.PI / 180.0; } /** * * GetDistance:根据两点间经纬度坐标(double值),计算两点间距离,单位为米 * @author zuyp * @version * @since Ver 1.1 * @Date 2013 2013-5-14 上午11:40:31 * @param @param lng1 * @param @param lat1 * @param @param lng2 * @param @param lat2 * @param @return * @return double * @throws */ public static double GetDistance(double lng1, double lat1, double lng2, double lat2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lng1) - rad(lng2); double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.round(s * 10000) / 10000; return s; } }
android 工具类2
猜你喜欢
转载自everlxq.iteye.com/blog/1979426
今日推荐
周排行