java 判断是否夏令时_怎么在linux下 使用java代码正确获取夏令时的时间

通过设备开通WiFi连接获取Mac地址是最可取的,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* 设备开通WiFi连接,通过wifiManager获取Mac地址

*

* @author 高焕杰

*/

public static String getMacFromWifi(Context context){

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

State wifiState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

if(wifiState == NetworkInfo.State.CONNECTED){//判断当前是否使用wifi连接

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

if (!wifiManager.isWifiEnabled()) { //如果当前wifi不可用

wifiManager.setWifiEnabled(true);

}

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

return wifiInfo.getMacAddress();

}

return null;

}

除了上面这种方法,网上还给出了另外两种方法:

1、通过调用Linux的busybox命令获取Mac地址:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

* 通过调用Linux的busybox命令获取Mac地址

*

* @author 高焕杰

*/

private static String getMacFromCallCmd(){

try {

String readLine = ;

Process process = Runtime.getRuntime().exec(busybox ifconfig);

BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(process.getInputStream()));

while ((readLine = bufferedReader.readLine ()) != null) {//执行命令cmd,只取结果中含有HWaddr的这一行

if(readLine.contains(HWaddr)){

return readLine.substring(readLine.indexOf(HWaddr)+6, readLine.length()-1);

}

}

}catch(Exception e) { //如果因设备不支持busybox工具而发生异常。

e.printStackTrace();

}

return null;

}

注意:这种方法在Android Pad中可以准确获取到的Mac地址,但是在Android手机中无法准确获取到。

2、通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/**

* 通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址

*

* @author 高焕杰

*/

private static String getMacFromFile(Context context){

String readLine =;

BufferedReader bufferedReader = null;

try {

bufferedReader = new BufferedReader(new FileReader(new File(/proc/net/arp)));

int rollIndex = 0;

while((readLine = bufferedReader.readLine())!=null){

if(rollIndex == 1){

break;

}

rollIndex = rollIndex + 1;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

if(readLine !=null && readLine.length()>1){

String[] subReadLineArray = readLine.split( );

int rollIndex = 1;

for(int i = 0; i < subReadLineArray.length; ++i){

if(!TextUtils.isEmpty(subReadLineArray[i])){

if(rollIndex == 4){

return subReadLineArray[i];

}

rollIndex = rollIndex + 1;

}

}

}

return null;

}

注意:无论在Android Pad中还是在Android手机中,这种方法都无法准确获取到Mac地址。

取消

评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值