question

Thamotharan-5143 avatar image
0 Votes"
Thamotharan-5143 asked RobCaplan edited

Is it possible to inheritace the SpeechRecognizer class in Xamarin?

I have trying to inheritance the SpeechRecognizer class using following code

 public class TestSpeechRecognizer : SpeechRecognizer
     {
            
         public TestSpeechRecognizer(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
         {
    
         }
         public override JniPeerMembers JniPeerMembers => base.JniPeerMembers;
    
         protected override IntPtr ThresholdClass => base.ThresholdClass;
    
         protected override Type ThresholdType => base.ThresholdType;
    
         public override void Cancel()
         {
             base.Cancel();
         }
    
         public override void Destroy()
         {
             base.Destroy();
         }
    
         public override bool Equals(object obj)
         {
             return base.Equals(obj);
         }
    
         public override bool Equals(Java.Lang.Object obj)
         {
             return base.Equals(obj);
         }
    
         public override int GetHashCode()
         {
             return base.GetHashCode();
         }
    
         public override void SetRecognitionListener(IRecognitionListener listener)
         {
             base.SetRecognitionListener(listener);
         }
    
         public override void StartListening(Intent recognizerIntent)
         {
             base.StartListening(recognizerIntent);
         }
    
         public override void StopListening()
         {
             base.StopListening();
         }
    
         public override string ToString()
         {
             return base.ToString();
         }
    
         protected override Java.Lang.Object Clone()
         {
             return base.Clone();
         }
    
         protected override void Dispose(bool disposing)
         {
             base.Dispose(disposing);
         }
    
         protected override void JavaFinalize()
         {
             base.JavaFinalize();
         }
     }

But I am getting following error on build.
error: SpeechRecognizer() has private access in SpeechRecognizer
public class CarrierSpeechRecognizer
126344-error-on-speechrecognizer.png




Please help me

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

Is it possible to inheritace the SpeechRecognizer class in Xamarin

No, we cannot create a class to extend the SpeechRecognizer class. When creating the custom class, it requires to add the constructor method like below:

public class TestSpeechRecognizer : SpeechRecognizer
{
    protected TestSpeechRecognizer(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }
}

In above code, we can see that the constructor will call the SpeechRecognizer's constructor method. Xamarin.Android is only a layer of wrapper for Android native libraries. In native Android's source code, SpeechRecognizer class only privdes the private constructor method which cannot be accessed just like the error describes.

//the part source code of the SpeechRecognizer class

private SpeechRecognizer(final Context context, final ComponentName serviceComponent)
{
    mContext = context;
    mServiceComponent = serviceComponent;
}

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Or is it possible to create event before Cancel or before Dispose?

0 Votes 0 ·

Or is it possible to create event before Cancel or before Dispose?

@Thamotharan-5143 The Cancel and Dispose are methods instead of the Event, we need to call the command manaually. You could just do the work before calling the method.

var recognizer = SpeechRecognizer.CreateSpeechRecognizer(this);
//preform the work

 recognizer.Cancel();
0 Votes 0 ·

Hi, @Thamotharan-5143
I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·