Monday, January 18, 2016

Checking if android emulator or device is running

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
adb devices
The above command will provide you the list of attached devices.
Example output,
List of devices attached 
192.168.56.101:5555 device
emulator-5554 device 
Programmatically:
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

5 comments:

  1. Thank you for sharing the information.It is very interesting blog on SAP Success Factors

    ReplyDelete
  2. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about APPIUM.
    http://thecreatingexperts.com/appium-training-in-chennai/

    ReplyDelete
  3. This is very informative blog and nice article , I really like your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near futureselenium training,selenium online training,selenium training online,selenium free course

    ReplyDelete