import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Locale; import org.apache.http.conn.util.InetAddressUtils; import android.app.Activity; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Environment; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; public class MyUtils { /** * 獲取屏幕寬度高度密度 * * 若是屏幕密度低,須要在工程的AndroidManifest.xml文件中,加入supports-screens節點 * <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true"/> * @param activity 屏幕界面 * @param type 類型1:寬度,2:高度,3:密度,4dpi密度 * @return 屏幕寬度或高度或密度 */ public Object getWindowSize(Activity activity,int type){ DisplayMetrics metric = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metric); if (type == 1) { int width = metric.widthPixels; // 屏幕寬度(像素) return width; }else if (type == 2) { int height = metric.heightPixels; // 屏幕高度(像素) return height; }else if (type == 3) { float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5) return density; }else if (type == 4) { int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240) return densityDpi; } return 0; } /** * 獲取MAC地址 * @param context 上下文 * @param replaceSymbol 替換字符,默認替換字符爲"" * @return 返回MAC地址 錯誤返回12個0 */ public String getMacAddress(Context context,String replaceSymbol) { String macAddress = "000000000000"; if (replaceSymbol == null) { replaceSymbol = ""; } try { WifiManager wifiMgr = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo info = (null == wifiMgr ? null : wifiMgr .getConnectionInfo()); if (null != info) { if (!TextUtils.isEmpty(info.getMacAddress())) macAddress = info.getMacAddress().replace(":", replaceSymbol); else return macAddress; } } catch (Exception e) { e.printStackTrace(); return macAddress; } return macAddress; } /** * 獲取當前時間 * @param type 日期時間格式 * @param locale 地區默認爲 Locale.CHINA * @return 按照格式返回當前時間 */ public String getCurrentTime(String type,Locale locale) { if (locale == null) { locale = Locale.CHINA; } Date curDate = new Date(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat(type,locale); return sdf.format(curDate); } /** * 日期格式轉換 * * @param date 待轉換日期 * @param type 格式 * @param locale 地區 默認爲 Locale.CHINA * @return 日期 */ public String formatDate(String date, String type,Locale locale) { String fmDate = ""; if (date != null) { if (locale == null) { locale = Locale.CHINA; } SimpleDateFormat sdf = new SimpleDateFormat(type,locale); fmDate = sdf.format(new Date(Long.parseLong(date))); } return fmDate; } /** * 獲取當前版本名,版本號 * @param context 上下文 * @param type 1:版本名稱,2:版本號 * @return 版本名或版本號 */ public Object getCurrentVersionName(Context context,int type){ PackageManager manager = context.getPackageManager(); String packageName = context.getPackageName(); String versionName = null; int versionCode = 0; try { PackageInfo info = manager.getPackageInfo(packageName, 0); if (type == 1) { versionName = info.versionName; return versionName; }else if (type == 2) { versionCode = info.versionCode; return versionCode; } } catch (NameNotFoundException e) { e.printStackTrace(); return null; } return null; } /** * 網絡檢測 * @param context 上下文 * @return false:無網絡,true:有網絡 */ public boolean isOnline(Context context) { boolean isOnline = false; final ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null) { isOnline = networkInfo.isAvailable(); } // String netType = "當前網絡類型爲:" + networkInfo.getTypeName(); return isOnline; } /** * 比較時間 * * @return true courseTime 大於當前時間 */ public boolean compareTime(String curTime, String courseTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.CHINA); boolean boo = true; try { boo = sdf.parse(courseTime).getTime() - sdf.parse(curTime).getTime() > 0; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return boo; } /** * 獲取有網線下的Ip地址 * 須要添加權限:<uses-permission android:name="android.permission.INTERNET" /> * @param context 上下文 * @return IP地址 */ public String getWXLocalIpAddress(Context context) { String ipv4 = "0.0.0.0"; try { boolean boo = true; List<NetworkInterface> nilist = Collections.list(NetworkInterface .getNetworkInterfaces()); for (NetworkInterface ni : nilist) { List<InetAddress> ialist = Collections.list(ni .getInetAddresses()); for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address .getHostAddress())) { boo = false; break; } if (!boo) { break; } } } } catch (SocketException ex) { Log.e("WangLuo", ex.toString()); } return ipv4; } /** * 獲取Wifi下的Ip地址 * 須要添加權限: <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> * @param context 上下文 * @return IP地址 */ public String getWifiLocalIpAddress(Context context) { WifiManager wifi = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); int ipAddress = info.getIpAddress(); return intToIp(ipAddress); } private String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF); } /** * SD卡是否存在 * @return */ public boolean isSDexist(){ //SD卡是否存在 boolean isExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); return isExist; } /** * 獲取網路鏈接類型 * @param context 上下文 * @return 網絡類型 * 須要添加權限<uses-permission android:name="android.permission.INTERNET"/> * 須要添加權限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> */ public String getNetType(Context context){ ConnectivityManager conn = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conn.getActiveNetworkInfo(); String result = null; if (info != null && info.isAvailable()) { if (info.isConnected()) { int type = info.getType(); String typeName = info.getTypeName(); switch (type) { case ConnectivityManager.TYPE_BLUETOOTH: result = "藍牙鏈接 : "+typeName; break; case ConnectivityManager.TYPE_DUMMY: result = "虛擬數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_ETHERNET: result = "以太網數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_MOBILE: result = "移動數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_MOBILE_DUN: result = "網絡橋接 : "+typeName; break; case ConnectivityManager.TYPE_MOBILE_HIPRI: result = "高優先級的移動數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_MOBILE_MMS: result = "運營商的多媒體消息服務 : "+typeName; break; case ConnectivityManager.TYPE_MOBILE_SUPL: result = "平面定位特定移動數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_WIFI: result = "Wifi數據鏈接 : "+typeName; break; case ConnectivityManager.TYPE_WIMAX: result = "全球微波互聯 : "+typeName; break; default: break; } } } return result; } }