获取设备信息

获取手机设备的IP地址:

知识延伸:

对于获取IP地址可以使用IPV4Address或者IPV6Address来获取,如果使用INetAddress来获取的话则调用getHostName,如果使用IPV4Address或者IPV6Address则通过调用getAllByName来获取。

IPV4地址格式:"1.2.3.4" - 1.2.3.4

IPV6地址格式:

1080:0:0:0:8:800:200C:417A  a unicast address
     FF01:0:0:0:0:0:0:101        a multicast address
     0:0:0:0:0:0:0:1             the loopback address
     0:0:0:0:0:0:0:0             the unspecified addresses
也可以是这种形式:

     1080::8:800:200C:417A       a unicast address
     FF01::101                   a multicast address
     ::1                         the loopback address
     ::                          the unspecified addresses
当然后面结尾也有可以会跟% 来表示域的ID,如: 1080::8:800:200C:417A%2  or  1080::8:800:200C:417A%en0

   /**
     * 得到网络接口的IP地址
     * @return
     */
    private String getDeviceIpAddress(){
        try {
            //NetworkInterface是用来获取本地的网络接口
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();//得到本地化网络接口
            while(en.hasMoreElements()){
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();//得到网络接口的地址
                while(enumIpAddr.hasMoreElements()){
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if(!inetAddress.isLoopbackAddress()){
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
获取手机设备的网络Mac地址:

  /**
     * 网络的Mac地址
     * @return
     */
    private String getDeviceMacAddress(){
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();//获取连接WIFI的信息
        String macAddress = wifiInfo.getMacAddress();
        return macAddress;
    }
获取CPU的信息:

    /**
     * 获取CPU的信息
     * @return
     */
    private String getCPUInfo(){
        String[] cpuInfo = {"",""};//1--CPU的型号   2---CPU的频率
        try {
            FileReader fileReader = new FileReader("/proc/cpuinfo");
            BufferedReader localBufferedReader = new BufferedReader(fileReader,8192);
            String strCPU = localBufferedReader.readLine();
            String[] arrayOfStringCPU = strCPU.split("\\s+");//正则表达式,表示根据空白字符进行分割
            for(int i = 2; i <arrayOfStringCPU.length; i++){
                cpuInfo[0] = cpuInfo[0] + arrayOfStringCPU[i] + "";
            }
            strCPU = localBufferedReader.readLine();//再次读取文件的下一行
            arrayOfStringCPU = strCPU.split("\\s+");
            cpuInfo[1] += arrayOfStringCPU[2];
            localBufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cpuInfo[0]+"   "+cpuInfo[1];
    }
获取手机内存的信息:

    /**
     * 得到手机的内存信息
     * @return
     */
    private String getMemoryInfo(){
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(memoryInfo);
        long mTotalMem = memoryInfo.totalMem;//0;
        long mAvailMem = memoryInfo.availMem;
        String totalMemory = Formatter.formatFileSize(this, mTotalMem);
        String availMemory = Formatter.formatFileSize(this, mAvailMem);
        return "totalMemory:"+totalMemory+"    availMemory:"+availMemory;
    }

    /**
     * 第二种方案,读取手机meeinfo文件的信息
     * @return
     */
    private String getMemoryInfo2(){

        String strMemory;
        long mTotalMem = 0;
        String[] arrayOfStringMemory;
        try {
            FileReader localFileReader = new FileReader("/proc/meminfo");
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            strMemory = localBufferedReader.readLine();
            arrayOfStringMemory = strMemory.split("\\s+");
            mTotalMem = Integer.valueOf(arrayOfStringMemory[1]).intValue() * 1024;
            localBufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Formatter.formatFileSize(this,mTotalMem);
    }
获取手机中的权限:

    /**
     * 获取手机中的权限
     * @return
     */
    private String getPermissionsInfo() {
        File path = new File("/etc/permissions/");
        File[] files = path.listFiles();
        ArrayList name = new ArrayList();
        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (fileName.endsWith(".xml")) {
                    name.add(fileName.substring(0,
                            fileName.lastIndexOf(".")).toString());
                }
            }
        }
        String permissionsInfo = "\nPermissions Info:\n";
        for (int i = 0; i < name.size(); i++) {
            permissionsInfo = permissionsInfo + name.get(i).toString() + "\n";
        }
        return permissionsInfo;
    }
获取本地时区、

语言、国家

   /**
     * 获取本地时区和语言、国家
     * @return
     */
    private String getLocaleInfo() {
        TimeZone timeZone = TimeZone.getDefault();
        String[] timeZoneArray = timeZone.toString().split("\"");
        Locale locale = getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        String country = locale.getCountry();
        String displayLanguage = locale.getDisplayLanguage();
        String displayCountry = locale.getDisplayCountry();
        return "TimeZone:" + timeZoneArray[1] + "\n" + "Language:" + language + "_" +
                country + " | " + displayLanguage + "(" + displayCountry + ")" + "\n";
    }

获取设备的ID:
    /**
     * 获取设备的Id
     * @return String
     */
    private String getTelephonyState() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        String subscriberId = telephonyManager.getSubscriberId();
        String phoneNumber = telephonyManager.getLine1Number();
        return "DeviceId:" + deviceId + "\nSubscriberId:" + subscriberId + "\nPhoneNumber:" + phoneNumber + "\n";
    }
手机传感器的信息列表:

//传感器的信息
        Sensor.TYPE_ACCELEROMETER//加速传感器
        Sensor.TYPE_AMBIENT_TEMPERATURE//温度计
        Sensor.TYPE_GAME_ROTATION_VECTOR//游戏旋转矢量传感器 
        Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR//地磁旋转矢量传感器
        Sensor.TYPE_GRAVITY//重力传感器
        Sensor.TYPE_GYROSCOPE//陀螺仪
        Sensor.TYPE_GYROSCOPE_UNCALIBRATED//未校准陀螺仪
        Sensor.TYPE_LIGHT//光线传感器
        Sensor.TYPE_LINEAR_ACCELERATION//加速度传感器
        Sensor.TYPE_MAGNETIC_FIELD//磁场传感器
        Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED//未校准磁场传感器
        Sensor.TYPE_ORIENTATION//方向传感器
        Sensor.TYPE_PRESSURE//压力传感器
        Sensor.TYPE_PROXIMITY//距离传感器
        Sensor.TYPE_RELATIVE_HUMIDITY//相对湿度传感器
        Sensor.TYPE_ROTATION_VECTOR//旋转矢量传感器
        Sensor.TYPE_SIGNIFICANT_MOTION//显著运动传感器
        Sensor.TYPE_STEP_COUNTER//计步传感器
        Sensor.TYPE_STEP_DETECTOR//步伐探测器
        Sensor.TYPE_TEMPERATURE//湿度传感器


你可能感兴趣的:(android)