Forum Xamarin.Android

How to display my dual sim phone numbers

monemdcmonemdc Member ✭✭✭
edited May 2020 in Xamarin.Android

hi

I want to get dual sim phone numbers . I want code work on android v 4.2 and hiegher . I used this code and did not work on Android devices

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.READ_PROFILE" />



       bt_.Click += delegate
                {

                    tm = (TelephonyManager)GetSystemService(TelephonyService);
                    String phonenumberstring = null;
                    phonenumberstring = tm.Line1Number;

                    Toast.MakeText(this, "phone " + phonenumberstring, ToastLength.Short).Show();
                };
Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You could try to use SubscriptionManager .

    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.Build.VERSION_CODES.LollipopMr1)
    {
        SubscriptionManager subscriptionManager1 = (SubscriptionManager)GetSystemService(Context.TelephonySubscriptionService);
        IList<SubscriptionInfo> subsInfoList = subscriptionManager1.ActiveSubscriptionInfoList;
    
        foreach (SubscriptionInfo subscriptionInfo in subsInfoList)
        {
            string numbers = subscriptionInfo.Number;
            Console.WriteLine("numbers====" + numbers);
        }
    }
    
  • monemdcmonemdc Member ✭✭✭
    edited May 2020

    @ColeX said:
    You could try to use SubscriptionManager .

    butto.Click += delegate
    {
    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.Build.VERSION_CODES.LollipopMr1)

                  {
                      SubscriptionManager subscriptionManager1 = (SubscriptionManager)GetSystemService(Context.TelephonySubscriptionService);
                      IList<SubscriptionInfo> subsInfoList = subscriptionManager1.ActiveSubscriptionInfoList;
    
                      foreach (SubscriptionInfo subscriptionInfo in subsInfoList)
                      {
                          string numbers = subscriptionInfo.Number;
                          //Console.WriteLine("numbers====" + numbers);
                          Toast.MakeText(this, "numbers====" + numbers, ToastLength.Short).Show();
                      }
                  }
              };
    

    No data was displayed

  • ColeXColeX Member, Xamarin Team Xamurai
    edited May 2020

    READ_PHONE_STATE permission is required , but it belongs to Dangerous permission , we need to request it from code .

            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneState) != (int)Permission.Granted)
            {
                RequestPermissions(new string[] { Manifest.Permission.ReadPhoneState }, 0);
            }
    

    Refer
    https://developer.android.com/reference/android/telephony/SubscriptionManager.html#getActiveSubscriptionInfoList()

    https://developer.android.com/training/permissions/requesting#request-permission

Sign In or Register to comment.