代码如下:
package com.java.app01; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class IsDevicesConnect { public static void main(String[] args) throws Exception { boolean bool = isDevicesConnect(); System.out.println(bool); } public static Boolean isDevicesConnect() { Process process; Runtime runtime = Runtime.getRuntime(); String driverName = ""; try { process = runtime.exec("adb devices"); InputStream inputStream = process.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GBK"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } driverName = stringBuilder.toString(); driverName = driverName.replaceAll("List of devices attached", ""); driverName = driverName.replaceAll("\n", ""); driverName = driverName.replaceAll("device", ""); } catch (Exception e) { e.printStackTrace(); } finally { if (driverName != null && driverName.length() != 0) { System.out.println("获取到的设备名是:" + driverName); return true; } return false; } } }