Android Question Moving a sub from your main module to a service module?

Discussion in 'Android Questions' started by aidymp, Aug 26, 2015.

  1. aidymp

    aidymp Active Member Licensed User

    Hi,

    I have a create a service module from a Sub in my main program. As my UI is using an animation to show the user that the app is working (similar to ProgressDialogShow) however as the sub is deleting a lot of files, in a folder, (100meg-2GB) This make the animation freeze for short periods. In a bid to make it look more professional. I would like a smooth animation. So its been suggested I use a service. I have never used a service myself and just want a little help rearranging the code!

    This is the Sub

    Code:
    Sub DeleteFolderRecursive(Folder As String)
       
    For Each f As String In File.ListFiles(Folder)
         
    If File.IsDirectory(Folder, f) Then
           DeleteFolderRecursive (
    File.Combine(Folder, f))
         
    End If
         
    File.Delete(Folder, f)
       
    DoEvents ' added to see its effect on the animation (without this the animation does not move, with it the animation is very jittery)
       Next
    End Sub
    So I have created a service module "FolderRemover"

    I understand to call the service I would use StartService(FolderRemover) but how would I pass the sting "Folder" to it?

    Also the structure of the Service Module "FolderRemover" Im unsure where the code goes. i currently have it here

    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False

    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Service_Create

    End Sub

    Sub Service_Start (StartingIntent As Intent)
    For Each f As String In File.ListFiles(Folder)
         
    If File.IsDirectory(Folder, f) Then
           DeleteFolderRecursive (
    File.Combine(Folder, f))
         
    End If
         
    File.Delete(Folder, f)
       
    DoEvents
       
    Next
    End Sub

    Sub Service_Destroy

    End Sub
    But cannot test as I cannot work out how to pass the "Folder" string to it!

    So does the code look correct? and how do i pass the "Folder" string?

    Sorry if I have it wrong, but I only used basic on the Atari ST and Amiga's so I have a lot of catching up to do.

    Thanks

    Aidy
     
    Last edited: Aug 26, 2015
  2. DonManfred

    DonManfred Expert Licensed User

    move the sub to you service

    Code:
    Sub DeleteFolderRecursive(Folder As String)
       
    For Each f As String In File.ListFiles(Folder)
         
    If File.IsDirectory(Folder, f) Then
           DeleteFolderRecursive (
    File.Combine(Folder, f))
         
    End If
         
    File.Delete(Folder, f)
       
    'DoEvents ' added to see its effect on the animation (without this the animation does not move, with it the animation is very jittery)
       Next
    End Sub
    In you app you start the service
    Code:
    StartService(servicename)
    directly followed by a
    Code:
    CallSubDelayed2(servicename,"DeleteFolderRecursive", Folder)
     
    aidymp likes this.
  3. aidymp

    aidymp Active Member Licensed User

    I thank you so much for the answer that was pretty painless! I Think however you can guess the next question ;)

    How do I know when it has completed? would I just define a Process_Global variable and then set a timer in my main program to check when it has completed? (by changing a flag after the Next command in the loop of the module?)

    Thanks

    Aidy
     
  4. DonManfred

    DonManfred Expert Licensed User

    Use callsubdelay in your Sub in the service to raise an event/ call a sub in your main activity after the service has done the job
     
    lemonisdead likes this.
  5. aidymp

    aidymp Active Member Licensed User

    Hi Thanks for that I have implemented it and it seemed to work once, now I get an Error. "Sub Unzip signature does not match expected signature"

    this is my deleter module

    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
       
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Service_Create

    End Sub

    Sub Service_Start (StartingIntent As Intent)

    End Sub

    Sub Service_Destroy

    End Sub
    Sub DeleteFolderRecursive(Folder As String)
       
    For Each f As String In File.ListFiles(Folder)
         
    If File.IsDirectory(Folder, f) Then
           DeleteFolderRecursive (
    File.Combine(Folder, f))
         
    End If
         
    File.Delete(Folder, f)
       
    DoEvents
       
    Next
       CallSubDelayed2(Main, 
    "Unzipper", Me)
    End Sub
    The Sub Unzipper is in my main program, am I calling it correctly?

    Thanks

    Aidy
     
  6. DonManfred

    DonManfred Expert Licensed User

    but you did not show WHAT the signature of your sub is....

    Hard to help without all needed informations.
     
  7. aidymp

    aidymp Active Member Licensed User

    That is possibly part of the problem! I am unaware that the sub has any signature?

    Code:
    Sub Unzipper
    apm1.Visible =
    False
    wv1.Visible=
    True
    ProgressDialogHide
    testo = Archiver1.NumberOfZipEntries(
    File.DirDefaultExternal , "/Download/Settings.zip")
    Wait (1)
    Label1.text = 
    "Extracting Setup!"
    apm1.Progress=
    0
    wv1.Visible=
    False
    apm1.BottomText=
    "Extracting..."
    apm1.Visible=
    True
    ProgressBar1.Progress=
    0
    Archiver1.AsyncUnZip(
    File.DirDefaultExternal , "/Download/Settings.zip"File.DirRootExternal & "/Android/data/org.blah.blah/files/blah/" , "prog")
    End Sub
    The only signature I know of is the one I sign my APK with! is that what you mean?

    Thanks

    Aidy
     
  8. walterf25

    walterf25 Well-Known Member Licensed User

    That is because when you are calling the unzipper sub you calling it like this

    Code:
    CallSubDelayed2(Main, "Unzipper", Me)
    you are passing the "Me" parameter, (not sure why you are doing that) but if you are passing a parameter then yous sub should look like this

    Code:
    Sub Unzipper(me as Object)
    'the rest of  your code goes here......
    End Sub
    if you don't want to pass any parameters to the sub then your code should look like this

    Code:
    CallSubDelayed(Main, "Unzipper")
    and your Unzipper sub can stay the way you have it right now!
    hope this helps
    Walter
     
    aidymp and DonManfred like this.
  9. DonManfred

    DonManfred Expert Licensed User

    Yes. Callsubdelay should be fine here...
     
    aidymp likes this.
  10. Jeffrey Cameron

    Jeffrey Cameron Active Member Licensed User

    Also, if you ever plan on obfuscating your build, I would recommend placing an underscore in the sub name, such:
    Code:
    Public Sub Un_Zipper
        
    ' your code
    End Sub
    Subs with underscores will not be obfuscated so the string literal in your service will still work (CallsubDelayed(Main, "Un_Zipper")).
     
    aidymp and walterf25 like this.
  11. aidymp

    aidymp Active Member Licensed User

    Hi! Thanks guys I understand now, I was lost in the terminology, the signature was the layout of the sub, ie Sub Name (Parameters) would use CallSubDelayed2 and with no parameters just callsubdelayed! hurray! Something else I have learned

    Thanks

    Aidy
     
    DonManfred and walterf25 like this.
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