2013-10-04 3 views
2

태블릿을 재부팅 한 후 앱을 실행하고 싶습니다 (설정). os.system을 사용할 수 있습니까? 아니면 다른 방법을 사용해야합니까?Python을 통해 Android 앱을 실행하는 방법

import os,time 

for i in range(0,3): 

    os.system("adb reboot") 
    time.sleep(60) 
+0

나는 그것을 발견 : ADB 쉘 원숭이 -p의 package.name -v 100 – user2661518

+0

체크 아웃이 [ADB 래퍼 (https://github.com/mdrabic/androidpy_tools/blob/development/adb.py) I 파이썬으로 썼다. 수행해야 할 adb 호출이 많은 경우 도움이 될 수 있습니다. – MDrabic

답변

2

예, os.system을 사용하여 ADB 명령을 실행할 수 있습니다. 성공적으로 실행 된 명령의 유효성을 검사하려면 subprocess 라이브러리 중 일부인 check_output(...) 함수를 살펴보십시오. 이 코드 스 니펫은 check_output 함수를 구현하는 방법을 선택합니다. 전체 코드보기는 here입니다.

def _run_command(self, cmd): 
""" 
Execute an adb command via the subprocess module. If the process exits with 
a exit status of zero, the output is encapsulated into a ADBCommandResult and 
returned. Otherwise, an ADBExecutionError is thrown. 
""" 
try: 
    output = check_output(cmd, stderr=subprocess.STDOUT) 
    return ADBCommandResult(0,output) 
except CalledProcessError as e: 
    raise ADBProcessError(e.cmd, e.returncode, e.output) 


는 명령 am start -n yourpackagename/.activityname을 사용할 수있는 응용 프로그램을 시작합니다. 설정 앱을 실행하려면 adb shell am start -n com.android.settings/com.android.settings.Settings을 실행하십시오. 이 stackoverflow question 명령 줄 의도를 통해 응용 프로그램을 시작하는 데 사용할 수있는 옵션을 자세히 보여줍니다.


다른 팁 :
나는 당신이 달성하려고하는 것에 도움이 될 수 몇 가지 다른 파이썬 유틸리티와 함께 ​​Python으로 작성된 ADB 래퍼를 만들었습니다. 예를 들어, time.sleep(60)을 호출하여 재부팅을 기다리는 대신 adb를 사용하여 sys.boot_completed 속성의 상태를 폴링하고 일단 속성이 설정되면 장치가 부팅을 끝내고 모든 응용 프로그램을 시작할 수 있습니다. 다음은 사용할 수있는 참조 구현입니다.

def wait_boot_complete(self, encryption='off'): 
""" 
When data at rest encryption is turned on, there needs to be a waiting period 
during boot up for the user to enter the DAR password. This function will wait 
till the password has been entered and the phone has finished booting up. 

OR 

Wait for the BOOT_COMPLETED intent to be broadcast by check the system 
property 'sys.boot_completed'. A ADBProcessError is thrown if there is an 
error communicating with the device. 

This method assumes the phone will eventually reach the boot completed state. 

A check is needed to see if the output length is zero because the property 
is not initialized with a 0 value. It is created once the intent is broadcast. 

""" 
if encryption is 'on': 
    decrypted = None 
    target = 'trigger_restart_framework' 
    print 'waiting for framework restart' 
    while decrypted is None: 
    status = self.adb.adb_shell(self.serial, "getprop vold.decrypt") 
    if status.output.strip() == 'trigger_restart_framework': 
     decrypted = 'true' 

    #Wait for boot to complete. The boot completed intent is broadcast before 
    #boot is actually completed when encryption is enabled. So 'key' off the 
    #animation. 
    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip() 
    print 'wait for animation to start' 
    while status == 'stopped': 
    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip() 

    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip() 
    print 'waiting for animation to finish' 
    while status == 'running': 
    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()   

else: 
    boot = False 
    while(not boot):  
    self.adb.adb_wait_for_device(self.serial) 
    res = self.adb.adb_shell(self.serial, "getprop sys.boot_completed") 
    if len(res.output.strip()) != 0 and int(res.output.strip()) is 1: 
     boot = True 
관련 문제