2

I want to get the mobile number from a different sim. Dual sim number is saved in a different variable.

Some people give answer like:

TelephonyManager tm =(TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number = tm.getLine1Number();

I also give the permission in the Manifest file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

but it didn't work, all time number variable give ("") value. So please provide mw with a solution to this problem.

Unheilig
  • 16,196
  • 193
  • 68
  • 98

2 Answers2

0

If tm.getLine1Number() returns null or "" try to use tm.getSubscriberId() it always worked for me.

String number = tm.getLine1Number();
if (number == null || number.equals("")) {
    number = tm.getSubscriberId();
}

Hope this helps.

DWattimena
  • 815
  • 8
  • 13
0

You can do this through the Subscription Manager for minSdkVersion 22 onwards :)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SubscriptionManager mSubscriptionManager = SubscriptionManager.from(getBaseContext());
        List<SubscriptionInfo> subscriptions = mSubscriptionManager.getActiveSubscriptionInfoList();


        for(SubscriptionInfo subscriptionInfo: subscriptions) {
            Log.v("SIM", subscriptionInfo.getNumber());
        }


    }
Roman Gherta
  • 821
  • 2
  • 15
  • 27
  • sir, i don't get value of my mobile number.... I have Dual sim mobile my first sim response like this :- {id=2, iccId=89918680400068704945 simSlotIndex=0 displayName=Jio 4G carrierName=Jio 4G nameSource=2 iconTint=-16746133 dataRoaming=0 iconBitmap=android.graphics.Bitmap@3cdba36c mcc 405 mnc 868 mSubStatus=1 mNwMode=9} – Shubham Maheshwari Jul 15 '17 at 11:37
  • 1
    @ShubhamMaheshwari i got this problem too, subscriptionInfo.getNumber() return null; – Jeffery Ma Aug 30 '17 at 09:01