2

I want to run any app (say Settings) after rebooting tablet. Can I use os.system or do I have to use other methods.

import os,time

for i in range(0,3):

    os.system("adb reboot")
    time.sleep(60) 
user2661518
  • 2,677
  • 9
  • 42
  • 79
  • I found it: adb shell monkey -p package.name -v 100 – user2661518 Oct 04 '13 at 17:27
  • Checkout this [adb wrapper](https://github.com/mdrabic/androidpy_tools/blob/development/adb.py) I wrote in python. It may help if you have a lot of adb calls to perform. – MDrabic Oct 04 '13 at 18:15

1 Answers1

2

Yes, you can use os.system to execute ADB commands. If you want to validate the command executed successfully, take a look at the check_output(...) function which is apart of the subprocess library. This code snipet is how I choose to implement the check_output function. For the full code look 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)


To launch an application you can use the command am start -n yourpackagename/.activityname. To launch the Settings App, run adb shell am start -n com.android.settings/com.android.settings.Settings. This stackoverflow question shows you in detail the options you can use to start the application via a command line intent.


Other tips:
I created an ADB wrapper written in python along with a few other python utilities that may aid in what you are trying to accomplish. For example, instead of calling time.sleep(60) to wait for the reboot, you use adb to poll the status of the property sys.boot_completed and once the property is set the device has finished booting and you can launch any application. Below is a reference implementation you can use.

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
Community
  • 1
  • 1
MDrabic
  • 917
  • 8
  • 15