开发者

How to get Telephone Number In Android

开发者 https://www.devze.com 2023-02-06 10:24 出处:网络
I want to get the telephone Number. I used this sample code.. TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE)开发者_运维技巧;

I want to get the telephone Number. I used this sample code..

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE)开发者_运维技巧;
String strphonenumber = tm.getLine1Number();

And I used the permission- READ_PHONE_STATE.

But its returning null.


You will only be able to get the Number from a CDMA device.

public String returnNumber() {
     String number = null;
     String service = Context.TELEPHONY_SERVICE;
     TelephonyManager tel_manager = (TelephonyManager) getSystemService(service);
     int device_type = tel_manager.getPhoneType();

     switch (device_type) {
           case (TelephonyManager.PHONE_TYPE_CDMA):
              number = tel_manager.getLine1Number();
           break;
           default:
             //return something else
             number = "no number";
            break;
     }
     return number;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消