如何获得手机没有设备的编程 [英] How to get phone no of device programmatically

查看:134
本文介绍了如何获得手机没有设备的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to get phone no using following method:
private String getMyPhoneNumber(){
        TelephonyManager mTelephonyMgr;

        mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
        //String imsi = mTelephonyMgr.getSubscriberId();
        String phnNo=mTelephonyMgr.getLine1Number();
        if (phnNo==null) {
            phnNo=  getNoFromWatsApp();
        }

        return phnNo;
    }
    private String  getNoFromWatsApp(){
        AccountManager am = AccountManager.get(this);
        Account[] accounts = am.getAccounts();
        String phoneNumber="";

        for (Account ac : accounts) {
            String acname = ac.name;
            String actype = ac.type;
            // Take your time to look at all available accounts
            if(actype.equals("com.whatsapp")){
                phoneNumber = ac.name;
            }
        }
        return phoneNumber;
    }

但每次我拿到电话号码的空白。我甚至试图从WATS应用程序,但它的returingWatsApp,而不是手机没有得到电话号码。 是否有解决这个问题的任何其他方式?

But every time i get phone no blank. I even tried to get phone no from wats app but its returing "WatsApp" instead of phone no. Is there any other way to solve this problem?

推荐答案

使用低于code:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

在AndroidManifest.xml中,赋予以下权限:

In AndroidManifest.xml, give the following permission:

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

但请记住,这code并不总是有效的,因为手机号码是依赖于SIM卡和网络运营商/手机运营商。

But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.

此外,尝试检查的电话 - >设置 - >关于 - >电话身份,如果你能够查看号码出现,概率从上面code得到的电话号码也较高。如果您无法查看设置的电话号码,那么你将无法通过此code搞定!

Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!

这篇关于如何获得手机没有设备的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆