Determine if emulator device is in use

与世无争的帅哥 提交于 2019-12-13 07:31:51

问题


Does anyone know what the method hasClients does in the android sdk?

boolean com.android.ddmlib.IDevice.hasClients()

It doesn't look like its documented.

I am trying to find a way to see if an emulator is being used. Any good way to do this?

for(int i =0; i < devices.length; i++){

    if(!devices[i].hasClients()){

        monkeyDevice = devices[i];

    }

}

When I say is being used, I mean if there is currently an application running on the device or if its receiving commands from anything.

Update

I should of mentioned that I want to test for these conditions outside from my application. I have a seperate class running outside the application that starts the application within an available emulator. I want this monkey class to know if an existing emulator is already being used for testing.


回答1:


Have a look at this question to figure out if you are running in the emulator:
How can I detect when an Android application is running in the emulator?

On a monkey-related point, you might want to have a look at Activity.isUserAMonkey() method (since API level 8, OS 2.2). The Google DeviceAdminSample code gives a brief explanation:

/**
 * If the "user" is a monkey, post an alert and notify the caller.  This prevents automated
 * test frameworks from stumbling into annoying or dangerous operations.
 */
private static boolean alertIfMonkey(Context context, int stringId) {
    if (ActivityManager.isUserAMonkey()) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(stringId);
        builder.setPositiveButton(R.string.monkey_ok, null);
        builder.show();
        return true;
    } else {
        return false;
    }
}


来源:https://stackoverflow.com/questions/7955499/determine-if-emulator-device-is-in-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!