如何使用UiAutomator关闭Android应用程序?

时间:2014-10-02 12:16:27

标签: android uiautomator

如何使用UiAutomator API关闭特定的Android应用? 就像当您手动按下“最近”按钮并滑动要关闭的应用程序一样。

6 个答案:

答案 0 :(得分:6)

更好的方式(不是设备,操作系统版本,UI或特定方向):

Runtime.getRuntime().exec(new String[] {"am", "force-stop", "pkg.name.of.your.app"});

使用Android 6.0测试和使用Nexus 5X

答案 1 :(得分:3)

最好的选择是使用getUiDevice.pressRecentApps,这将为您加载最近的应用程序,然后使用uiautomator查看器截取屏幕截图,您可以查看已加载的屏幕的xml。然后,您可以使用此xml选择要使用

滑动的对象
UiObject app = new UIObject(new UiSelector().resourceId("The id of the app");
app.swipeLeft(100); 

或者正确

这应该可以关闭你的应用。 xml将取决于您使用的Android风格和设备。

答案 2 :(得分:0)

当它只是一个应用程序将出现在最近的应用程序列表中时,这对我有用。

if(mDevice.pressRecentApps()) {
            Thread.sleep(1000);
            int startX = 300; int startY =835; int endX = 1000; int endY = 835; // co-ordinates refer to x-axis from left of screen to right.
            int steps = 8;// speed at which the app closes
            mDevice.swipe(startX,startY,endX,endY,steps);
        }

答案 3 :(得分:0)

这就是我用uiautomator一次杀死所有Android应用程序的方法:

public static void killApps()
{
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    try
    {
        device.pressHome();
        device.pressRecentApps();

        // Clear all isn't always visible unless you scroll all apps down
        int height = device.getDisplayHeight();
        int width = device.getDisplayWidth();
        device.swipe(width/2,height/2, width/2, height, 50);

        UiObject clear = device.findObject(new UiSelector()
            .resourceId("com.android.systemui:id/button")
            .text("CLEAR ALL")
        );
        if (clear.exists())
        {
            clear.click();
        }
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
    catch (UiObjectNotFoundException e)
    {
        e.printStackTrace();
    }
}

答案 4 :(得分:0)

基于@ user597159的解决方案,我获得了以下内容,以关闭用于Firebase测试实验室的Pixel 2(即“ walleye”设备类型)上的所有应用程序:

        cachef_h = [a,b,c,d]
        f = open('cachef_h.txt', 'x')
           f.write(cachef_h[0])
           for column_headers in cachef_h[1:]:
           f.write(',' + column_headers)

注意Pixel 2的拼写为“全部清除”,而不是“全部清除”。

我无法使用其他一些解决方案。我得到private void killAllApps() throws Exception { boolean keepSwiping = true; int maxSwipeAttempts = 10; for (int swipeAttempt=0; swipeAttempt<maxSwipeAttempts && keepSwiping; swipeAttempt++) { int height = uiDevice.getDisplayHeight(); int width = uiDevice.getDisplayWidth(); uiDevice.swipe(width / 2, height / 2, width, height / 2, 50); UiObject clearAll1 = uiDevice.findObject(new UiSelector().text("Clear all")); UiObject clearAll2 = uiDevice.findObject(new UiSelector().textStartsWith("Clear all")); UiObject clearAll3 = uiDevice.findObject(new UiSelector().textContains("Clear all")); UiObject clear = clearAll1.exists() ? clearAll1 : (clearAll2.exists() ? clearAll2 : clearAll3); if (clear.exists()) { Logger.debug(TAG, "Attempting to close app by killAllApps and found clear=all button on swipeAttempt=" + swipeAttempt); clear.click(); keepSwiping = false; } else { Logger.debug(TAG, "Attempting to close app by killAllApps but have to keep swiping swipeAttempt=" + swipeAttempt); keepSwiping = true; } } } 的以下信息:

UiObjectNotFoundException

还有:

app = uiDevice.findObject(new UiSelector().textContains("SDK Test App"));

换句话说,app = uiDevice.findObject(new UiSelector().className(com.locuslabs.android.sdk.SdkTestApplication.class)); 对于尝试在应用程序上向上滑动以在Pixel 2上关闭的这些方法返回了false。

答案 5 :(得分:0)

这是Kotlin的答案,类似于this answer。它向UiDevice添加了扩展功能以清除所有任务。我在Pixel 2模拟器上对此进行了测试。

fun UiDevice.clearAllTasks(swipeAttempts: Int = 10 ) {
    pressRecentApps()

    var currentAttempt = 1
    val centerScreenX = displayWidth / 2
    val centerScreenY = displayHeight / 2
    while (currentAttempt <= swipeAttempts) {
        Timber.d("Clear all tasks attempt $currentAttempt")
        swipe(centerScreenX, centerScreenY, displayWidth, centerScreenY, 50)
        val uiObject = findObject(UiSelector().text("Clear all"))
        if (uiObject.exists()) {
            uiObject.click()
            break
        } else {
            currentAttempt++
        }
    }
}