从Python-Django启动Android模拟器

5 投票
5 回答
3783 浏览
提问于 2025-04-16 16:09
def start_test(request):
    os.system('echo Starting emulator...')
    os.system('./android-sdk-linux_x86/tools/emulator -avd testavd &')
    return HttpResponse("OK")

这是我想要实现的基本代码。
当这段代码运行时,服务器在运行模拟器的时候就不再响应了。希望能得到一些帮助。
我正在使用Django开发服务器。以下是服务器的输出:

Django version 1.1.1, using settings 'Cloust.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Starting emulator...
[21/Apr/2011 02:00:06] "GET /start_test/a.apk/ HTTP/1.1" 200 5
emulator: warning: opening audio output failed

emulator: emulator window was out of view and was recentred

5 个回答

1

如果你在用Django框架的话,可能需要以某种方式来管理模拟器。

我觉得在这种情况下,使用线程并不是一个好主意。

我建议你可以看看任务管理的相关内容,比如这个链接:http://code.google.com/p/django-tasks/

2

也许你可以试着在一个单独的线程中运行模拟器?

比如说:

import subprocess
thread = threading.Thread(target=subprocess.popen(['./android-sdk-linux_x86/tools/emulator', '-avd', 'testavd', '&'])
thread.start()
0

我还没有好好解决这个问题,不过使用subprocess.Popen让我可以在模拟器上执行命令:

print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')

值得一提的是,这里使用的是django的开发服务器,而这个服务器是通过sudo启动的,所以显然这并不是最理想的做法。

撰写回答