Google told me that in order for me to be able to use either TelephonyManager or TelcomManager to read device identifiers I need one of the requirements stated on the documentation for the respective device identifiers such as GetImei
or GetLine1Number
. I decided to meet the requirement that states The caller needs to be the default SMS Role holder on that device
. So I created an app that is listed as an alternative SMS app on my device and then I check if my app holds that role and then try to get the device identifiers. My code reads the IMEI
of both sim cards successfully but fails to read the phone number of the device as the method returns null but I did not expect this as the default sms app should be able to access that number for texting purposes. I am using the following code, help me make this work.
try
{
if (roleManager.IsRoleHeld(RoleManager.RoleSms))
{
var manager = (TelephonyManager)GetSystemService(Context.TelephonyService);
new Android.App.AlertDialog.Builder(this).SetTitle("Device Identifiers")
.SetMessage("phone no " + manager.Line1Number + "\n" + "line 1 imei: " + manager.GetImei(0) + "\n" +
"line 2 imei: " + manager.GetImei(1) + "\n" +
"serial no: " + manager.SimSerialNumber
).Show();
//telephonyManager.GetLine1 fails so
//try and use the telecom service to read the number
var telcom = (TelecomManager)GetSystemService(Context.TelecomService);
//get a list of all capable calling accounts
IList<PhoneAccountHandle> handles = telcom.CallCapablePhoneAccounts;
if (handles != null)
{
//Toast the phone capable calling accounts count
Toast.MakeText(this, handles.Count.ToString(), ToastLength.Short).Show();
//get the phone account handle in index 0
PhoneAccountHandle handle1 = handles[0];
//get the phone number associated with that acount
string phone1 = telcom.GetLine1Number(handle1);
if (phone1 != null)
{
new Android.App.AlertDialog.Builder(this).SetTitle("Phone Number")
.SetMessage(phone1).Show();
}
}
string phone = telcom.GetLine1Number(handles[1]);
if (phone != null)
{
Toast.MakeText(this,phone,ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "GetLine1 is returning null", ToastLength.Short).Show();
}
}
} catch (Java.Lang.SecurityException exc)
{
Toast.MakeText(this, exc.Message, ToastLength.Short).Show();
}
An answer in Android Java is also acceptable, Thank You.