2013-10-04 55 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)予在Python中寫道。如果您有很多adb電話需要執行,這可能會有所幫助。 – MDrabic

回答

2

是的,您可以使用os.system來執行ADB命令。如果要驗證執行成功的命令,請查看check_output(...)函數,它是subprocess函數庫的一部分。此代碼snipet是我如何選擇實現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。這個計算器流程question詳細顯示了您可以通過命令行意圖啓動應用程序的選項。


其他提示:
我創建一個用Python編寫的幾個其他的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 
相關問題