Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2011 SOFTEC sa. All rights reserved.
 *
 * This source code is licensed under the Creative Commons
 * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
 * License.
 *
 * To view a copy of this license, visit
 * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
 * or send a letter to Creative Commons, 171 Second Street,
 * Suite 300, San Francisco, California, 94105, USA.
 */

import java.io.*;

public class Main {
    public static boolean isAndroidEmulatorRunning(File androidSdkHome) throws IOException {
        if (androidSdkHome == null) {
            return false;
        }

        File adb = new File(androidSdkHome, "platform-tools" + File.separator + "adb");
        if (!adb.exists()) {
            return false;
        }

        boolean isEmulatorRunning = false;

        ProcessBuilder pb = new ProcessBuilder(adb.getAbsolutePath(), "devices");
        pb.redirectErrorStream(true);
        Process p = pb.start();

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith("List of devices")) {
                continue;
            } else if (line.startsWith("emulator-")) {
                String[] tokens = line.split("\\s");
                String name = tokens[0];
                String status = tokens[1];
                int port = Integer.parseInt(name.substring(name.indexOf("-") + 1));
                if (status.equals("device") && port == 5560) {
                    isEmulatorRunning = true;
                }
            }
        }

        return isEmulatorRunning;

    }
}