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

aidymp

Well-Known Member
Licensed User
Longtime 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

B4X:
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

B4X:
#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:

DonManfred

Expert
Licensed User
Longtime User
move the sub to you service

B4X:
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
B4X:
     StartService(servicename)
directly followed by a
B4X:
 CallSubDelayed2(servicename,"DeleteFolderRecursive", Folder)
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
move the sub to you service

B4X:
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
B4X:
     StartService(servicename)
directly followed by a
B4X:
 CallSubDelayed2(servicename,"DeleteFolderRecursive", Folder)

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
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime 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

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

B4X:
#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
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
but you did not show WHAT the signature of your sub is....

Hard to help without all needed informations.

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

B4X:
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
 
Upvote 0

walterf25

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

B4X:
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
That is because when you are calling the unzipper sub you calling it like this

B4X:
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

B4X:
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

B4X:
CallSubDelayed(Main, "Unzipper")

and your Unzipper sub can stay the way you have it right now!
hope this helps
Walter
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Also, if you ever plan on obfuscating your build, I would recommend placing an underscore in the sub name, such:
B4X:
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")).
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
That is because when you are calling the unzipper sub you calling it like this

B4X:
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

B4X:
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

B4X:
CallSubDelayed(Main, "Unzipper")

and your Unzipper sub can stay the way you have it right now!
hope this helps
Walter

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
 
Upvote 0
Top