Hi
I'm trying to get used to using services for general async calls back to our server from an B4A app.
The idea is what when a user requests something on an Activity, we kick off a service to perform it and have the service callback the activity on completion.
Based on other threads I've read, I'm trying to implement the following type of solution:
In my main activity I do something like this:
------------------------------------------------------------------
The on my service I do something like this:
My problem is that when the CallSubDelayed2 calls the Activity.LoginComplete ... it resumes the activity.btnLogin_Click at the ProgressDialogHide line which is good. But the code in the LoginComplete sub doesn't seem to execute (the logs don't write and the bLoginSuccess is unchanged.
I've noticed too some slightly different results by moving the bLoginSuccess into the ProcessGlobals but not the desired result.
So I've completely confused myself on how this process should be coded to get the desired effect.
The desired effect is:
From an activity call a sub in a service to start an async process and wait for a call back to the activity with some variable passed back with details of the outcome.
Any ideas where I'm going wrong?
I'm trying to get used to using services for general async calls back to our server from an B4A app.
The idea is what when a user requests something on an Activity, we kick off a service to perform it and have the service callback the activity on completion.
Based on other threads I've read, I'm trying to implement the following type of solution:
In my main activity I do something like this:
B4X:
Sub Globals
Dim bLoginSuccess As Boolean
End Sub
Sub btnLogin_Click
Log("frmLogin(), btnLogin_Click()")
ProgressDialogShow("Attempting sign in ...")
CallSubDelayed2(svc_wsh_login, "Login", Me)
Wait For LoginComplete
ProgressDialogHide
If bLoginSuccess Then
Log("frmLogin(), btnLogin_Click(),bLoginSuccess, ActivityFinish ")
Activity.Finish
Else
Log("frmLogin(), btnLogin_Click(),bLoginSuccess failed")
End If
End Sub
Sub LoginComplete(Result As String) As String
Log("frmLogin(), LoginComplete(), " & Result)
If Result = "SUCCESS" Then
bLoginSuccess = True
Else
bLoginSuccess = False
End If
Log(Result)
LoginComplete Result
End Sub
------------------------------------------------------------------
The on my service I do something like this:
B4X:
Sub Process_Globals
Dim CallbackActivity As Object
End Sub
Sub Login(Callback As Object)
Log("svc_wsh_login(), Login()")
CallbackActivity = Callback
End Sub
Sub wsh_ServerLoginResult(Params As List)
Log("svc_wsh_login(), wsh_ServerLoginResult")
If Params.Get(0) = "FAILED" Then
Log("svc_wsh_login(), wsh_ServerLoginResult, Failed")
ToastMessageShow("Login Failed",True)
CallSubDelayed2(CallbackActivity, "LoginComplete","FAIL")
Else
Log("svc_wsh_login(), wsh_ServerLoginResult, Success")
CallSubDelayed2(CallbackActivity, "LoginComplete","SUCCESS")
End If
StopService("") 'Stop service as I don't believe its needed anymore
End Sub
My problem is that when the CallSubDelayed2 calls the Activity.LoginComplete ... it resumes the activity.btnLogin_Click at the ProgressDialogHide line which is good. But the code in the LoginComplete sub doesn't seem to execute (the logs don't write and the bLoginSuccess is unchanged.
I've noticed too some slightly different results by moving the bLoginSuccess into the ProcessGlobals but not the desired result.
So I've completely confused myself on how this process should be coded to get the desired effect.
The desired effect is:
From an activity call a sub in a service to start an async process and wait for a call back to the activity with some variable passed back with details of the outcome.
Any ideas where I'm going wrong?