2

タブレットを再起動した後、任意のアプリ (設定など) を実行したい。os.system他の方法を使用できますか、または使用する必要がありますか?

import os,time

for i in range(0,3):

    os.system("adb reboot")
    time.sleep(60) 
4

1 に答える 1

2

はい、os.systemADB コマンドの実行に使用できます。正常に実行されたコマンドを検証したい場合は、サブプロセスライブラリcheck_output(...)の一部である関数を見てください。このコード スニペットは、関数を実装するために選択した方法です。完全なコードについては、こちらをご覧ください。check_output

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の質問は、コマンドラインインテントを介してアプリケーションを起動するために使用できるオプションを詳細に示しています.


その他のヒント:
私は Python で書かれた ADB ラッパーを、あなたが達成しようとしていることに役立つ他のいくつかの Python ユーティリティと共に作成しました。たとえば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
于 2013-10-04T18:43:19.370 に答える