此文是为了记录一下在开发4GQoS测速应用中用到的一些方法。
1、将背景图和状态栏融合到一起:
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
2、获取本机Ip地址:
public String getLocalIpAddress() {
//Enumeration是一个接口,NetworkInterface.getNetworkInterfaces() 返回的是一个泛型的枚举器,元素类型是NetworkInterface
//NetworkInterface表示物理硬件和虚拟地址
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
Enumeration<InetAddress> enIp = ni.getInetAddresses();
while (enIp.hasMoreElements()) {
//InetAddress是ip地址的高级表示
InetAddress inet = enIp.nextElement();
//isLoopbackAddress():指示指定的ip地址是否为回环地址
if (!inet.isLoopbackAddress() && inet instanceof Inet4Address) {
return inet.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "0.0.0.0";
}
3、获取手机唯一标识:
public String getLocalDeviceId(Context context){
TelephonyManager tm = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
String number = tm.getDeviceId();
return number;
}
注:要获取本机号码,可将getDeviceId()改为getLine1Number(),但是此方法有时不一定能用,因为运营商为了保护客户隐私,可能不会提供手机号码。
4、Ping命令:
/**
* ping ip, 然后获得时延,抖动,丢包率值
*
* @param ipAddress
* @param coutCmd
* @return ArrayList<String>
*/
private ArrayList<String> pingIpAddress(String ipAddress, String coutCmd) {
ArrayList<String> array = new ArrayList<>();
final String ping = "ping -c " + coutCmd + " " + ipAddress;
InputStream in = null;
BufferedReader reader = null;
try {
process = Runtime.getRuntime().exec(ping);
in = process.getInputStream();
pingStatus = process.waitFor();
reader = new BufferedReader(new InputStreamReader(in));
String lineStr;
if (pingStatus == 0) {
//ping成功
while ((lineStr = reader.readLine()) != null) {
Log.i(TAG, "pingIpAddress: " + lineStr);
if (lineStr.contains("packet loss")) {
int i = lineStr.indexOf("received");
int j = lineStr.indexOf("%");
lost = lineStr.substring(i + 10, j + 1);
array.add(lost);
}
if (lineStr.contains("avg")) {
int i = lineStr.indexOf("/", 20);
int j = lineStr.indexOf(".", i);
delay = lineStr.substring(i + 1, j);
array.add(delay);
}
if (lineStr.contains("mdev")) {
mdev = lineStr.substring(lineStr.lastIndexOf("/") + 1, lineStr.indexOf("ms"));
mdev = mdev.substring(0, mdev.indexOf(".") + 2);
array.add(mdev);
}
}
return array;
} else {
//ping失败
while ((lineStr = reader.readLine()) != null) {
Log.i(TAG, "pingIpAddress: " + lineStr);
if (lineStr.contains("packet loss")) {
int i = lineStr.indexOf("received");
int j = lineStr.indexOf("%");
lost = lineStr.substring(i + 10, j + 1);
array.add(lost);
}
}
array.add("∞");
array.add("∞");
return array;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}