xamarinxamarin.androidtelephonymanager

Why is Xamarin class inaccessible due to protection level?


I am trying to access the telephony manager class in Xamarin forms. I read up on the constructor needed to be used in Xamarin.

While I am still a little confused on the types the constructor needs, and the reasoning, I put together a couple constructor args that I thought should work. I am getting an error that:

TelephonyManager(IntPtr, JNIHandleOwnership) is inaccessible due to its protection level

How can this be? I can't control the protection level of the C# version of this Android class as far as I know.

using Android.Telephony;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CellularSignal
{
    public class MainActivityViewModel
    {
        public double getRSSI()
        {
            IntPtr ptr = IntPtr.Zero;
            // **Intellisense error on line below**
            var t = new TelephonyManager(ptr, JniHandleOwnership.DoNotTransfer);
            return 0;

        }
    }
}

Solution

  • Something like this should work:

    TelephonyManager tpMgr = (TelephonyManager)mAppContext.GetSystemService(Context.TelephonyService);
    

    where mAppContext is the current Activity that you are using in your app, if you are in one of its Fragments then you can get the activity from its Activity property.