安卓檢測網線是否連接的方式

        //檢查網線有沒有插
	private boolean checkLan(){
		Process p=null;
		BufferedReader in=null;
		try {
            p = Runtime.getRuntime().exec("cat /sys/class/net/eth0/carrier");
        	in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
            String line = null;  
			String value = "";
            while ((line = in.readLine()) != null) {
				Log.i("tag", "carrier is "+line);
				if("0".equals(line)){
					return false;
				}
				if("1".equals(line)){
					return true;
				}
            }
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }
        finally{
        	if(in!=null){
        		try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        	}
        }
		return false;
	}

根據節點的值可以判斷網線有沒有插入有線網口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章