赞
踩
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
- //运用反射得到build类里的字段
- Field[] fields = Build.class.getDeclaredFields();
- //遍历字段名数组
- for (Field field : fields) {
- try {
- //将字段都设为public可获取
- field.setAccessible(true);
- //filed.get(null)得到的即是设备信息
- haspmap.put(field.getName(), field.get(null).toString());
- Log.d("CrashHandler", field.getName() + " : " + field.get(null));
- } catch (Exception e) {
- }
- }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- /**获得IP地址,分为两种情况,一是wifi下,二是移动网络下,得到的ip地址是不一样的*/
-
- public static String getIPAddress() {
- Context context=MyApp.getContext();
- NetworkInfo info = ((ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
- if (info != null && info.isConnected()) {
- if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
- try {
- //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
- for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
- NetworkInterface intf = en.nextElement();
- for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
- InetAddress inetAddress = enumIpAddr.nextElement();
- if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
- return inetAddress.getHostAddress();
- }
- }
- }
- } catch (SocketException e) {
- e.printStackTrace();
- }
-
- } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
- WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- //调用方法将int转换为地址字符串
- String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
- return ipAddress;
- }
- } else {
- //当前无网络连接,请在设置中打开网络
- }
- return null;
- }
-
- /**
- * 将得到的int类型的IP转换为String类型
- *
- * @param ip
- * @return
- */
- public static String intIP2StringIP(int ip) {
- return (ip & 0xFF) + "." +
- ((ip >> 8) & 0xFF) + "." +
- ((ip >> 16) & 0xFF) + "." +
- (ip >> 24 & 0xFF);
- }

- public static String getMacAddr()
- {
- /*
- * 获取mac地址有一点需要注意的就是android
- * 6.0版本后,以下注释方法不再适用,不管任何手机都会返回"02:00:00:00:00:00"
- * 这个默认的mac地址,这是googel官方为了加强权限管理而禁用了getSYstemService
- * (Context.WIFI_SERVICE)方法来获得mac地址。
- */
- // String macAddress= "";
- // WifiManager wifiManager = (WifiManager)
- // MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
- // WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- // macAddress = wifiInfo.getMacAddress();
- // return macAddress;
-
- String macAddress = "02:00:00:00:00:00";
- StringBuffer buf = new StringBuffer();
- NetworkInterface networkInterface = null;
- try
- {
- networkInterface = NetworkInterface.getByName("wlan0");
- if (networkInterface == null)
- {
- networkInterface = NetworkInterface.getByName("eth1");
- }
- if (networkInterface == null)
- {
- return "02:00:00:00:00:00";
- }
- byte[] addr = networkInterface.getHardwareAddress();
- if (addr == null || addr.length < 1)
- {
- return "02:00:00:00:00:00";
- }
- for (byte b : addr)
- {
- buf.append(String.format("%02X:", b));
- }
- if (buf.length() > 0)
- {
- buf.deleteCharAt(buf.length() - 1);
- }
- macAddress = buf.toString();
- }
- catch (SocketException e)
- {
- e.printStackTrace();
- return "02:00:00:00:00:00";
- }
- return macAddress;
- }

- /**获取手机的IMEI号码*/
- public static String getPhoneIMEI() {
- TelephonyManager mTm = (TelephonyManager) MyApp.getContext().getSystemService(Context.TELEPHONY_SERVICE);
- String imei = mTm.getDeviceId();
- String imsi = mTm.getSubscriberId();
- String mtype = android.os.Build.MODEL; // 手机型号
- String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得
- return imei;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。