monkeyrunner入门之各种按键(五)

这里我已一个音乐播放器为例,
monkeyrunner入门之各种按键(五)
KeyCode:
home键 KEYCODE_HOME
back键 KEYCODE_BACK
send键 KEYCODE_CALL
end键 KEYCODE_ENDCALL
上导航键 KEYCODE_DPAD_UP
下导航键 KEYCODE_DPAD_DOWN
左导航 KEYCODE_DPAD_LEFT
右导航键 KEYCODE_DPAD_RIGHT
ok键 KEYCODE_DPAD_CENTER
上音量键 KEYCODE_VOLUME_UP
下音量键 KEYCODE_VOLUME_DOWN
power键 KEYCODE_POWER
camera键 KEYCODE_CAMERA
menu键 KEYCODE_MENU

可复制代码:

#coding:utf-8

#保存文件格式记得选择utf-8,默认是ansi

# 引入本程序所用到的模块
import time
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage


# 连接手机设备

device = MonkeyRunner.waitForConnection()


#打开app, component="包名/activity名"
#aapt dump badging apk包      可得到package: name 和  launchable-activity: name
device.startActivity(component="com.meditation.deepsleep.relax/com.cs.bd.relax.activity.SplashActivity")

#暂停5秒等app完全打开
MonkeyRunner.sleep(5)

#按下back键
device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
print("back")
MonkeyRunner.sleep(3)

#打开app
device.startActivity(component="com.meditation.deepsleep.relax/com.cs.bd.relax.activity.SplashActivity")
MonkeyRunner.sleep(5)

#播放歌曲
device.touch(270,1000,'DOWN_AND_UP')
MonkeyRunner.sleep(3)
device.touch(550,860,'DOWN_AND_UP')
MonkeyRunner.sleep(3)
print("open music")

#调高音量
for i in range(1,5): 
	device.press('KEYCODE_VOLUME_UP', MonkeyDevice.DOWN_AND_UP)
	MonkeyRunner.sleep(2)
print("high voice")
MonkeyRunner.sleep(3)

#调低音量
for i in range(1,5): device.press('KEYCODE_VOLUME_DOWN', MonkeyDevice.DOWN_AND_UP)
print("short voice")
MonkeyRunner.sleep(3)


#回到主页
device.press('KEYCODE_HOME',MonkeyDevice.DOWN_AND_UP)
print("over")

#锁屏键
device.press('KEYCODE_POWER', MonkeyDevice.DOWN_AND_UP)
print("power")
MonkeyRunner.sleep(3)