I am trying to read the phone number in android. I've tried with these codes, but i couldn't get the number.Every one is suggesting this code only,but its not working why?

Suggestions please

Thanks for your precious time!...

MainActvity.java

btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            phonenumber = mTelephonyMgr.getLine1Number();

    System.out.println(">>>----- Phone number-----" + phonenumber);


        }
    });

Manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
有帮助吗?

解决方案

You can check Settings -> About Phone -> Status -> My Phone number in your mobile. This number is returned by getLine1Number(). If it is Unknown, then getLint1Number() will return null or "". Itseems it is Operator/SIM dependent.

其他提示

Getting phone Number is dependent on sim .For most devices (TelephonyManager.getLine1Number()) will work.For some it may not.You replace with another sim and Check it..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top