1

This is the following code to retrieve phone number wwas able to get it on samsung 4.0.4, but getting below error on htc one v mobile..any clue?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textDeviceID = (TextView)findViewById(R.id.deviceid);


TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

textDeviceID.setText(getMy10DigitPhoneNumber(telephonyManager));


}
private String getMyPhoneNumber(){
 TelephonyManager mTelephonyMgr;
 mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 return mTelephonyMgr.getLine1Number();
 }

 private String getMy10DigitPhoneNumber(TelephonyManager telephonyManager){
 String s = getMyPhoneNumber();
 return s.substring(0);
 }
}
hari86
  • 659
  • 2
  • 16
  • 28
  • 1
    Please edit your post and include the error message. – gh. Sep 05 '12 at 09:35
  • 1
    There is no reliable way to get the phone number. `getLine1Number()` relies on the phone number being stored on the SIM card. Some operators do this and some don't. Also, if the user changes his phone number, the phone number you get from the SIM card using `getLine1Number()` will be wrong. There are dozens of posts like this on StackOverflow. For example see http://stackoverflow.com/questions/5134398/telephonymanager-getline1number-failing/10255465#10255465 – David Wasser Sep 05 '12 at 10:14
  • Thanks david..but atleast i should return null right but iam getting error messages in logcat – hari86 Sep 05 '12 at 10:24

2 Answers2

0

According to the documentation .getLine1Number() "Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable. "

Apparently .getLine1Number() reads this information from SIM card, so if the operator has set the MSISDN field it will return you its value and null if they did not set this field.

In your case probably your SIM card does not have this field populated by operator.

Sayyam
  • 959
  • 10
  • 22
  • @sayyam-is there any way to find the MSISDN number whether its set or not?..and its not returning null..giving errors... – hari86 Sep 05 '12 at 10:08
0
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mblNumber = mTelephonyMgr.getLine1Number();

Note: Dont forget to add READ_PHONE_STATE permission to be added inside the AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Valeh Ağayev
  • 556
  • 4
  • 12