Android Question VoiceRecognition from a Service Module?

Discussion in 'Android Questions' started by leitor79, Aug 12, 2019.

  1. leitor79

    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!
     
  2. Erel

    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.
     
  3. leitor79

    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:

    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. leitor79

    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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Try to reproduce it in a small project and upload it.
     
  7. leitor79

    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:

    Code:
    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,
     
  8. Erel

    Erel Administrator Staff Member Licensed User

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