简体   繁体   English

如何获得xamarin表格中的电话号码?

[英]How to get phone number in xamarin forms?

I am working on xamarin.forms. 我正在研究xamarin.forms。 I need to get the phone number, if the device is dual sim then I need to get both the numbers. 我需要获取电话号码,如果设备是双卡,那么我需要得到这两个号码。 How do I achieve this? 我该如何实现这一目标? There so many questions related to this after referring all I came here. 在提到我来到这里之后,有很多与此相关的问题。

I created one interface in shard project and implemented the interface in an android project. 我在shard项目中创建了一个接口,并在android项目中实现了接口。 android.permission.READ_PHONE_NUMBERS android.permission.READ_PHONE_NUMBERS

created interface in shared project 在共享项目中创建接口

public interface IDeviceInfo
{
 string GetMyPhoneNumber();
}

Implemented the interface in android project 在android项目中实现了接口

public string GetMyPhoneNumber()
{
   try
   {
      TelephonyManager mgr =     
  Android.App.Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
    return mgr.Line1Number;
   }
  catch (Exception ex)
  {
  throw;
  }
}

On one of Button click on shared project 在其中一个Button上单击共享项目

 private void GetPhone_Clicked(object sender, EventArgs e)
 {  
    try
    {

        var data = DependencyService.Get<IDeviceInfo>().GetMyPhoneNumber();
    }
    catch (Exception ex)
    {
          throw;
    }
}

When I am using android 5 it is returning the empty string and if I am using android 6+ it is giving permission error like Java.Lang.SecurityException: getLine1NumberForDisplay: Neither user 10286 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS 当我使用android 5时,它返回空字符串,如果我使用的是android 6+,则会给出权限错误,如Java.Lang.SecurityException:getLine1NumberForDisplay:既不是用户10286也不是当前进程hasandroid.permission.READ_PHONE_STATE,android.permission。 READ_SMS或android.permission.READ_PHONE_NUMBERS

How do resolve this in all android version? 如何在所有Android版本中解决这个问题?

For iOS, this is not possible and even if you somehow get it using CoreTelephony or something else your application would get rejected for apple store deployment with the below issue as stated by Dylan here : 对于iOS,这是不可能的,即使你使用它某种方式得到CoreTelephony或别的东西,你的申请将被拒绝了苹果商店部署,由迪伦指出以下问题在这里

"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on." “出于安全原因,iPhone OS将应用程序(包括其首选项和数据)限制在文件系统中的唯一位置。此限制是安全功能的一部分,称为应用程序的”沙箱“。沙箱是一组很好的 - 粒度控制限制应用程序访问文件,首选项,网络资源,硬件等。“

The device's phone number is not available within your application's container. 设备的电话号码在应用程序的容器中不可用。 You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store. 您需要将应用程序修改为仅在目录容器中读取,然后将二进制文件重新提交到iTunes Connect,以便重新考虑App Store的应用程序。

Now for Android , you could use the TelephonyManager approach to get the phone number, but in my knowledge, it is not a reliable approach to do so it does not get the phone number from the sim details or something it picks it up from the device information that you enter at the phone's startup an explanation for it is present here 现在对于Android ,您可以使用TelephonyManager方法获取电话号码,但据我所知,这不是一个可靠的方法,它不会从SIM卡的详细信息中获取电话号码或从设备中获取电话号码您在手机启动时输入的信息, 此处有解释

Also, there is an important comment here that seems to be relevant 此外,这里有一个重要的评论似乎是相关的

Actually, not so perfect. 实际上,并不是那么完美。 Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. 上次我尝试这种方法时,它报告了我的手机原来的电话号码,在我的旧手机号码被移植到它之前。 It probably still does, as the Settings app still shows that defunct number. 它可能仍然可以,因为设置应用程序仍然显示已解散的数字。 Also, there are reports that some SIMs cause this method to return null. 此外,有报告称某些SIM会导致此方法返回null。 That being said, I'm not aware of a better answer. 话虽如此,我不知道更好的答案。

Even if this somehow is fine with you, you can only get one phone number using this and this phone number would be null most of the times if your user is not configured in the mobile's settings. 即使这种情况对您来说很好,但您只能使用此电话号码获得一个电话号码,如果您的用户未在移动设备中配置,则此电话号码大多数时间都为空。

Possible solution (only if you ask me) 可能的解决方案(只有你问我)

The way in which both these issues could get solved is, creating a screen something like a pop-up or a page according to your convenience that asks the user himself to enter the phone number if mandatory make a modal page all together 这些问题可以解决的方法是,根据您的方便创建一个类似弹出窗口或页面的屏幕,要求用户自己输入电话号码,如果必须一起制作模态页面

Good luck 祝好运

Revert in case of queries 在查询时还原

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM