安卓检测网线是否插入

发布时间 2023-07-17 17:15:31作者: xiaowang_lj
        //检查网线有没有插
    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;
    }