Android 获得系统版本等信息

package com.iptv.stv.supertv.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;

 
public class SystemUtil {
    private static final String TAG = "SystemParamUtil";
    private static Class gPropertyClass = null;
    private static Method gPropertyGet = null;
    private static Method gPropertySet = null;

    public static Class[] getParamTypes(Class cls, String mName) {
        Class[] cs = null;

        Method[] mtd = cls.getDeclaredMethods();
        for (int i = 0; i < mtd.length; i++) {
            if (!mtd[i].getName().equals(mName)) {
                continue;
            }

            cs = mtd[i].getParameterTypes();
            if (cs != null && cs.length == 1)
                break;
        }
        return cs;
    }

    public static void getPropertyClass(){
        try {
            gPropertyClass = Class.forName("android.os.SystemProperties");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void getPropertyGetMethod() {
        if(gPropertyClass != null){
            Class[] parameterTypes = getParamTypes(gPropertyClass, "get");
            try {
                gPropertyGet = gPropertyClass.getMethod("get", parameterTypes);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }

    public static void getPropertySetMethod() {
        if(gPropertyClass != null){
            Class[] parameterTypes = getParamTypes(gPropertyClass, "set");
            try {
                gPropertyGet = gPropertyClass.getMethod("set", parameterTypes);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取有线 mac 地址
     * @return          有线 mac 地址 or null
     */
    public static String getEth0MacAddress() {
        String macSerial = "";
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/eth0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str;) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();
                    break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return macSerial;

    }


    public static String getLanguage(){
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage()+ "-" + locale.getCountry();
        String ret = null;
        if (language.endsWith("zh-CN")) {
            ret = "zh";
        } else if (language.endsWith("zh-TW")) {
            ret = "zh_hk";
        } else if (language.endsWith("zh-HK")) {
            ret = "zh_hk";
        } else if (language.startsWith("en")) {
            ret = "en";
        } else {
            ret = "zh_hk";
        }
        return ret;
    }


    public static String getWifiMacAddress(Context context) {
        String macAddress = "";
        if(Build.VERSION.SDK_INT >= 23 ) {
            Enumeration<NetworkInterface> interfaces;
            try {
                interfaces = NetworkInterface.getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface iF = interfaces.nextElement();
                    if("wlan0".equals(iF.getName())) {
                        byte[] addr = iF.getHardwareAddress();
                        if (addr == null || addr.length == 0) {
                            continue;
                        }
                        StringBuilder buf = new StringBuilder();
                        for (byte b : addr) {
                            buf.append(String.format("%02X:", b));
                        }
                        if (buf.length() > 0) {
                            buf.deleteCharAt(buf.length() - 1);
                        }
                        macAddress = buf.toString();
                        return  macAddress;
                    }
                }
            } catch (SocketException e) {
                return macAddress;
            }
        } else {
            WifiManager wifiMgr = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
            if (null != info) {
                macAddress = info.getMacAddress();
            }
        }
        return macAddress;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值