简体   繁体   English

如何在Android中获取手机SIM卡号(GSM)

[英]How to get Mobile sim number(GSM) in android

I am developing an Android application. 我正在开发一个Android应用程序。 In that i need to get the user mobile sim number for calling purposes automatically. 在这种情况下,我需要自动获取用于呼叫目的的用户手机SIM卡号码。 But i am facing the problem in getting the mobile number of the user. 但是我在获取用户的手机号码时面临着问题。 I have tried this. 我已经试过了。

          TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String getSimSerialNumber = telemamanger.getSimSerialNumber();

    String deviceID = telemamanger.getDeviceId();
    String getSimNumber = telemamanger.getLine1Number();

and this 和这个

          TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    String network;
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(cm.getActiveNetworkInfo().getTypeName().equals("MOBILE"))
        network = "cellnetwork/4G";
    else if(cm.getActiveNetworkInfo().getTypeName().equals("WIFI"))
        network = "wifi";
    else
        network ="na";

   String uphone = tm.getLine1Number();
    Toast.makeText(getApplicationContext(),uphone+" "+network,Toast.LENGTH_LONG).show();

In these, i am getting all the information i want except the mobile number. 在这些信息中,我得到了除手机号码以外我想要的所有信息。 Is there any alternate method to get mobile number like in BHIM app. 是否有其他替代方法来获取手机号码,例如在BHIM应用中。 In BHIM app they are getting the mobile numbers automatically(whether it is single or dual sim). 在BHIM应用中,他们会自动获取手机号码(无论是单卡还是双卡)。

Sure! 当然!

These are some informations that you can get both on SubscriptionManager and SubscriptionInfo. 这些是您可以在SubscriptionManager和SubscriptionInfo上获得的一些信息。

Though I'm not really sure on what permissions you need to do that on a 3rd party app. 尽管我不太确定您需要在第三方应用程序上执行的权限。 This is how I do on my company's framework: 这是我对公司框架的处理方式:

private void fillSimNumber(TelephonyManager tm, SubscriptionInfo subscriptionInfo) {
    final String rawNumber =  tm.getLine1Number(subscriptionInfo.getSubscriptionId());

    if (!TextUtils.isEmpty(rawNumber)) {
        mSimNumber.setText(rawNumber);
    }
}

Best of Luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM