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;
}
}
Android 获得系统版本等信息
最新推荐文章于 2024-04-22 14:49:05 发布