We can check if an android device or an emulator is attached(plugged in) or running before we start using the attached device.
Command line:
We can check the following command to check list of attached devices
Example output,
We can do this check programmatically also as below
Command line:
We can check the following command to check list of attached devices
adb devicesThe above command will provide you the list of attached devices.
Example output,
List of devices attached 192.168.56.101:5555 device emulator-5554 deviceProgrammatically:
We can do this check programmatically also as below
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/"; private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
/** * Checks if an emulator or a device is already launched or plugged in * * Example: * List of devices attached * 192.168.56.101:5555 device * emulator-5554 device * * @return */ public static boolean isEmulatorOrDeviceRunning() { try { String[] commandDevices = new String[] { adbPath, "devices" }; Process process = new ProcessBuilder(commandDevices).start(); BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); String output = ""; String line = null; while ((line = inputStream.readLine()) != null) { System.out.println(line); output = output + line; } if (!output.replace("List of devices attached", "").trim().equals("")) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; }If the device or emulator is just plugged in or launched, it may not be ready though its running, so wait till it gets ready by the post Waiting for android emulator to be ready