Android Question wait for in ServletResponse

zmrcic

Member
Licensed User
Longtime User
This is what I have so far. small server, created in service.
problem is handling the response, because I must start intent based on the received message in the request.

B4X:
Sub HandleMainPage (Response As ServletResponse)
Dim rs As ResumableSub = CallSub(Main,"Testing")
    If rs.IsInitialized Then
        Wait For (rs) Complete (Result As Object)
        Log("AFTER WAITING")
       
        Response.SetContentType("text/html")
        Response.SendString("<b>its ok</b>")
       
    Else
        Log("Activity was paused")
    End If

and in main I have testing sub that starts an intent


B4X:
Dim in As Intent
   
    in.Initialize("android.intent.action.RINGTONE_PICKER", "")
    in.PutExtra("android.intent.extra.ringtone.TYPE", 1)

    Dim ActivityForResult As StartActivityForResult
    ActivityForResult.Initialize("Ion")
    ActivityForResult.Start(in,100)
    Log("wwww1")
   
    Dim intent_result As Object
   
    Wait For Ion_OnActivityResult (RequestCode As Int, ResultCode As Int, Data As Intent, ExtraParams() As Object)
    If ResultCode = StartActivityForResult.RESULT_OK And RequestCode = 100 Then
    .
    .


I read that I can not call intent from a service, so I have to do it like this.
In Postman, the request is waiting as long as the code gets to the line

Wait For (rs) Complete (Result As Object)

How can I wait for intent response and send it back wia response?
Code is based on Erel's example somewhere from the forum. In my case, I do not have to use RINGTONE_PICKER but third-party app. That went OK and I'm able to get an intent response, but sending it back is problem.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 1

zmrcic

Member
Licensed User
Longtime User
Thanks, Don. StartMessageLoop is not B4A, or am I wrong?
My app waits as long as it takes, I can see Postman is waiting for a response, but at the moment code enters

Wait For Ion_OnActivityResult (RequestCode As Int, ResultCode As Int, Data As Intent, ExtraParams() As Object)

connection to postman(user) is broken. response is 200(OK), but I want app to wait for intent response and send that to the user....
maybe becuase I have one wait for in HandleMainPage and another one in Wait For Ion_OnActivityResult when starting intent?

use upper code. you will see connection closed on that line of code. Why?
or maybe I do not want to now why, only to overcome this problem :)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The problem may be that you are creating a RS object from Main and Main is (most likely) destroyed once Main calls the 3rd party app's intent and the 3rd party app goes to top.

Try something like
B4X:
Sub HandleMainPage (Response As ServletResponse)
'Dim rs As ResumableSub = CallSub(Main,"Testing")
'    If rs.IsInitialized Then
    if IsPaused(Main) = False Then
        CallSub(Main, "Testing")
        Wait For Ion_Callback(Result As Int) ' Make the method name anything you want, make the parameters anything CallSubDelayed[2|3] can handle
        Log("AFTER WAITING")
       
        Response.SetContentType("text/html")
        Response.SendString("<b>its ok</b>")
       
    Else
        Log("Activity was paused")
    End If

Then in Main
B4X:
Dim in As Intent
   
    in.Initialize("android.intent.action.RINGTONE_PICKER", "")
    in.PutExtra("android.intent.extra.ringtone.TYPE", 1)

    Dim ActivityForResult As StartActivityForResult
    ActivityForResult.Initialize("Ion")
    ActivityForResult.Start(in,100)
    Log("wwww1")
   
    Dim intent_result As Object
   
    Wait For Ion_OnActivityResult (RequestCode As Int, ResultCode As Int, Data As Intent, ExtraParams() As Object)
    If ResultCode = StartActivityForResult.RESULT_OK And RequestCode = 100 Then
        CallSubDelayed2(WhatEverTheServiceIsCalled, "Ion_Callback", ResultCode) ' Make sure WhatEverTheServiceIsCalled is the name of the
                                                          ' service that has the HandleMainPageSub, the sub name match
                                                          ' (in this case Ion_Callback) and the passed parameter types match
                                                          ' (in this case Int).
'May need more checks to ensure service is already/still up and running before using CallSubDelayed2 (otherwise may still get some strange results)                                                                                                       
    .
    .
 
Upvote 0

zmrcic

Member
Licensed User
Longtime User
thx Oliver, but can not make it to work...
maybe I'm doing something wrong?

what is Ion_Callback?
I get it that I have to declare it in service....
or maybe not.

have you tested your example? is it working OK?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Not tested. Ion_callback is just a name you use with wait for that you can use with callsubdelayed. You can name it anything, you just have to use the same naming convention in the wait for statement and the callsubdelayed method.
 
Upvote 0

zmrcic

Member
Licensed User
Longtime User
HttpServer library posted by Erel will not work in the background, as far as I can tell.
If I only put the main activity in background, httpserver will not accept any requests. So I guess the problem is not in intents and wait for, its httpserver.
To be honest, it was not made for the scenario I need.

Is there something that can accept requests but works in background? Some socket, or something....
 
Upvote 0
Top