Android Question VoiceRecognition from a Service Module?

leitor79

Active Member
Licensed User
Hi,

I've tried to use the voice recognition from a service module, and it doesn't work. I movedt he code to an activity module and it works, so I think it has to be with the functions not working form a service; is that right?

I don't care about the google screen showing up (if it goes away after the recording), but can I call it directly from the service, without showing the activity? (I'm calling it from a widget)

Thank you!
 

Erel

Administrator
Staff member
Licensed User
VoiceRecognition is based on calling StartActivityForResult. This can only be done from an activity context.

You can start your own activity (it can be transparent) and then call VR.Listen.
 

leitor79

Active Member
Licensed User
Thank you, Erel!
I've done that, but I've a problem. I've created a new activity but, after the recognition, the Main activity opens? This is the code from my secondary activity:

B4X:
Sub VR_Result (Success As Boolean, Texts As List)
    Dim s As String
    s=""
    If Success = True Then
        For i = 0 To Texts.Size -1
            Log(Texts.Get(i))
            s = s & Texts.Get(i) & CRLF
        Next
        ToastMessageShow(s,True)
    Else
        ToastMessageShow("ERROR",False)
    End If
    Activity.Finish
End Sub
 

leitor79

Active Member
Licensed User
Hi Erel, thank you for your answer.
I've pasted that code, but it doesnt take me back to the home screen; it takes me back to the main activity and, moreover, again to the voice recognition acitivity. I'm stucked in a loop VR activity -> main activity after the VR -> VR activity -> main activity after the VR... and so on.
 

Erel

Administrator
Staff member
Licensed User
Try to reproduce it in a small project and upload it.
 

leitor79

Active Member
Licensed User
Hi Erel, thank you for your answer.

The project is the same as this one
I don't remember if the VR_Result sub is already modified with your previous suggestion but, in case not, please go to layRecord activity (yes, I had a prefix lapsus when naming it) and replace VR_Result with this one:

B4X:
Sub VR_Result (Success As Boolean, Texts As List)
    Dim s As String
    s=""
    If Success = True Then
        For i = 0 To Texts.Size -1
            Log(Texts.Get(i))
            s = s & Texts.Get(i) & CRLF
        Next
        ToastMessageShow(s,True)
    Else
        ToastMessageShow("ERROR",False)
    End If
    
    Dim in As Intent
    in.Initialize(in.ACTION_MAIN, "")
    in.AddCategory("android.intent.category.HOME")
    in.Flags = 0x10000000
    StartActivity(in)
    
End Sub
Thank you very much,
 
Top