如何使用 Monkeyrunner API 制作 Java 应用程序?

发布于 2024-11-19 16:47:53 字数 91 浏览 4 评论 0原文

Android SDK 有一个名为 Monkeyrunner 的 API,用于向手机发送命令。它似乎是一个Python API。我可以在 Java 应用程序中使用它吗?

The Android SDK has an API for sending commands to the phone called Monkeyrunner. It appears to be a Python API. Is there anyway I can use it in a Java application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-11-26 16:47:53

好吧,我一直在尝试这样做,这就是我发现的(感谢谷歌和互联网上成员的一些帮助)

这是一个小Java程序,它使用monkeyrunner来打印设备的名称

import com.android.monkeyrunner.MonkeyDevice;
import com.android.monkeyrunner.adb.AdbBackend;

public class Monk {

 public static void main(String[] args) {
    // TODO code application logic here
    Monk monk=new Monk();
    monk.demo();
 }
 public void demo()
 {
    AdbBackend ab = new AdbBackend();
    MonkeyDevice device = ab.waitForConnection();
    //Print Device Name       
    System.out.println(device.getProperty("build.model"));
    device.dispose();
 }

}

对于上面的代码也可以工作,我需要包含以下 jars Monkeyrunner、ddmlib、jython、guavalib、sdklib。

Well I have been trying to do this, here is what I found (Thanks to google and some help from members on the internet)

Here is a little Java program that uses monkeyrunner to print the name of the device

import com.android.monkeyrunner.MonkeyDevice;
import com.android.monkeyrunner.adb.AdbBackend;

public class Monk {

 public static void main(String[] args) {
    // TODO code application logic here
    Monk monk=new Monk();
    monk.demo();
 }
 public void demo()
 {
    AdbBackend ab = new AdbBackend();
    MonkeyDevice device = ab.waitForConnection();
    //Print Device Name       
    System.out.println(device.getProperty("build.model"));
    device.dispose();
 }

}

For the above code too work, I needed to include the following jars monkeyrunner, ddmlib, jython, guavalib, sdklib.

ゃ懵逼小萝莉 2024-11-26 16:47:53

以下是 @Harkish 答案的更新,它适用于我认为是 MonkeyRunner 的当前版本:

import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;

public class MonkeyTest {
    public static void main(String[] args) {
        // sdk/platform-tools has to be in PATH env variable in order to find adb
        IChimpDevice device = new AdbBackend().waitForConnection();

        // Print Device Name
        System.out.println(device.getProperty("build.model"));

        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("out.png", null);

        device.dispose();
    }
}

库依赖项是:

chimpchat.jar, common.jar, ddmlib.jar, guava-13.0.1.jar, sdklib.jar

它们都可以在 ADT 的 sdk/tools/lib 子目录中找到捆。

Here is an update to @Harkish's answer which works with what I assume to be the current version of MonkeyRunner:

import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;

public class MonkeyTest {
    public static void main(String[] args) {
        // sdk/platform-tools has to be in PATH env variable in order to find adb
        IChimpDevice device = new AdbBackend().waitForConnection();

        // Print Device Name
        System.out.println(device.getProperty("build.model"));

        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("out.png", null);

        device.dispose();
    }
}

The library dependencies are:

chimpchat.jar, common.jar, ddmlib.jar, guava-13.0.1.jar, sdklib.jar

They can all be found in the sdk/tools/lib subdirectory of the ADT bundle.

荒岛晴空 2024-11-26 16:47:53

我正在提供另一个更新的答案。这也是谷歌开发人员建议的。我认为这是一个更可靠的实现,并且它使用了更多的故障安全方法。

import java.util.Map;
import java.util.TreeMap;
import com.android.chimpchat.ChimpChat;
import com.android.chimpchat.core.IChimpDevice;


public class MonkeyRunnerTest {

private static final String ADB = "/path-to-your-sdk/sdk/platform-tools/adb";
private static final long TIMEOUT = 5000;

/**
 * @param args
 */
public static void main(String[] args) {

       Map<String, String> options = new TreeMap<String, String>();
       options.put("backend", "adb");
       //this is so you don't need to add adb or platform-tools to your system path
       options.put("adbLocation", ADB);
       ChimpChat chimpchat = ChimpChat.getInstance(options);
       //Using this method is advised as to avoid hangs,as this would wait indefinitely
       //Actually waitForConnection() doesn't wait indefinitely but for Integer.MAX_VALUE milliseconds, which still makes up for 596 hours
       IChimpDevice device = chimpchat.waitForConnection(TIMEOUT, ".*");
       chimpchat.shutdown();
    }
  }

您可以通过以下方式查看所有设备属性:

for (String prop: device.getPropertyList()) {
    System.out.println(prop + ": " + device.getProperty(prop));
}

有关 API 的信息,您可以查看此处的文档: Monkey runner api 类

I'm jumping in to provide yet another updated answer.This is what a google dev advised as well.I think it is a more solid implementation and it uses more fail-safe methods.

import java.util.Map;
import java.util.TreeMap;
import com.android.chimpchat.ChimpChat;
import com.android.chimpchat.core.IChimpDevice;


public class MonkeyRunnerTest {

private static final String ADB = "/path-to-your-sdk/sdk/platform-tools/adb";
private static final long TIMEOUT = 5000;

/**
 * @param args
 */
public static void main(String[] args) {

       Map<String, String> options = new TreeMap<String, String>();
       options.put("backend", "adb");
       //this is so you don't need to add adb or platform-tools to your system path
       options.put("adbLocation", ADB);
       ChimpChat chimpchat = ChimpChat.getInstance(options);
       //Using this method is advised as to avoid hangs,as this would wait indefinitely
       //Actually waitForConnection() doesn't wait indefinitely but for Integer.MAX_VALUE milliseconds, which still makes up for 596 hours
       IChimpDevice device = chimpchat.waitForConnection(TIMEOUT, ".*");
       chimpchat.shutdown();
    }
  }

You can see all the devices properties with:

for (String prop: device.getPropertyList()) {
    System.out.println(prop + ": " + device.getProperty(prop));
}

For information on the APIs you can look at the docs here: monkey runner api classes

海未深 2024-11-26 16:47:53

只是为了补充 ValarDohaeris 的精彩答案,这里是 maven 格式的当前依赖项:

<properties>
    <com.android.tools.version>24.3.1</com.android.tools.version>
</properties>

<dependencies>
    <dependency>
        <groupId>net.sf.sociaal</groupId>
        <artifactId>chimpchat</artifactId>
        <version>22.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>sdklib</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>common</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>sdk-common</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools.ddms</groupId>
        <artifactId>ddmlib</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>
</dependencies>

Just to complement the great answer from ValarDohaeris, here it is the current dependencies in maven format:

<properties>
    <com.android.tools.version>24.3.1</com.android.tools.version>
</properties>

<dependencies>
    <dependency>
        <groupId>net.sf.sociaal</groupId>
        <artifactId>chimpchat</artifactId>
        <version>22.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>sdklib</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>common</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools</groupId>
        <artifactId>sdk-common</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>

    <dependency>
        <groupId>com.android.tools.ddms</groupId>
        <artifactId>ddmlib</artifactId>
        <version>${com.android.tools.version}</version>
    </dependency>
</dependencies>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文