Laser
Laser

Reputation: 6960

Monkeyrunner activity

I have one problem, I has started 2 activities via monkeyrunner.
How I can close one of them?
I try to used device.stopActivity(package/activity), but monkeyrunner have not these command.
And also i want to recieve all working activities how i can do that?

Upvotes: 4

Views: 3772

Answers (2)

Victor Sh
Victor Sh

Reputation: 1

Hi better option is to stop the process

device.press('KEYCODE_HOME', MonkeyDevice.DOWN_AND_UP)
kill_command = 'am force-stop %s' % package_name
device.shell(kill_command)

Upvotes: 0

Gabriel Porumb
Gabriel Porumb

Reputation: 1691

As far as I know, Android manages the activities on his own. But there's a way to stop the activities via the kill command. Use a python script to do this:

First, simulate the touch of the home button:

device.press('KEYCODE_HOME', MonkeyDevice.DOWN_AND_UP)

Then use the ps command to list the open processes:

processes = str(device.shell('ps'))

Then look in the processes variable to see if you package is still there. If it's there, find the line which contains the package and retrieve it's PID. Then use the PID to kill the process:

if package in processes:
    for line in processes.splitlines():
        if package in line:
            pid = line.split()[1]
device.shell('kill ' + pid)

Later edit: I have found another way in which you can close/ stop a package:

device.shell('am force-stop package')

The am force-stop command needs the package name only as an argument. It closes the respective package.

Upvotes: 7

Related Questions