Is it possible to inheritace the SpeechRecognizer class in Xamarin?

Thamotharan 21 Reputation points
2021-08-25T12:28:44.733+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2021-08-26T06:42:22.96+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.