Android Question Execute bash script?

Discussion in 'Android Questions' started by Dustin Tinsley, Nov 8, 2016.

  1. Dustin Tinsley

    Dustin Tinsley Member Licensed User

    I am trying to create a simple app that will run a bash script and then execute an app. The bash script does some downloads behind the scene that updates data for the app. However, I can not get it to work correctly. I know the bash script is functioning correctly because it works properly if I adb into the device and run it that way. Here is what I have thus far:

    ADB into device and run: adb shell /sdcard/update.sh - WORKS
    Run script from app (code below) - FAILS, DOES NOT GET NEEDED INFO

    Here is my code from my Main activity:

    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Try
            
    Dim update As Phone
            
    Dim kodi As Intent
            
    Dim pm As PackageManager
            update.Shell(
    "sh /sdcard/update.sh"NullNullNull)
            
    Wait(10)
            
    Msgbox ("Try it now""")
            
    Catch
              
    ToastMessageShow ("Failed to launch app!  Is it installed?"True)
        
    End Try
        
    Activity.Finish
    End Sub

    Sub Wait(Seconds As Int)
       
    Dim Ti As Long
       Ti = 
    DateTime.Now + (Seconds * 1000)
       
    Do While DateTime.Now < Ti
          
    DoEvents
       
    Loop
    End Sub
    And here is the code that calls it:
    Code:
    Sub Activity_Create(FirstTime As Boolean)

        
    'This activity is REQUIRED but has to be left alone, you can name it whatever you want
                    'but the name MUST match the one in the manifest.
                    StartActivity(Main)
                    
    Activity.Finish

    End Sub
    I just started using B4A so I am sure I have something simple wrong, but can't figure it out..

    The bash script I am running does some downloads, then executes another bash script on /sdcard/ but like I said, that all works if I use ADB to execute the script...
     
  2. Dustin Tinsley

    Dustin Tinsley Member Licensed User

    I should add I have added these two permissions to the app via manifest editor:

    AddPermission(android.permission.READ_EXTERNAL_STORAGE)
    AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Never try to block the main thread. It will not work.

    Use a timer or CallSubPlus instead.

    Why do you need to run a script? You can download whatever needed from your app directly.
     
  4. Dustin Tinsley

    Dustin Tinsley Member Licensed User

    Ok, I feel stupid. Worked all weekend on this simple app. Gave up and started over a few times. Woke up this morning to look at your reply and instantly noticed something. I was missing the INTERNET permission! Now, to clean up the code. Like I said, I am new to B4A. What do you mean by "block the main thread"? I am a little lost there.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The Wait sub should be removed.
     
  6. Dustin Tinsley

    Dustin Tinsley Member Licensed User

    Ah, gotcha. Yeah, I already got rid of it. I was using it to determine wait until the script was done but discovered that using "string = update.Shell("sh /sdcard/update.sh", Null, Null, Null)" waited until script was complete.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice