Android Note Series--Get Mobile Number

The reason why some mobile phones cannot obtain mobile phone numbers:
    Not all mobile phone numbers can be obtained. Only part of it is available. This is because the mobile operator did not write the data of the mobile phone number into the SIM card. The SIM card has only a unique number, which is the IMSI number for the network and the device to identify. The signal of the mobile phone can also be said to be transmitted in the network through this number. Yes, not a phone number. Just imagine, after your SIM is lost, will you change the number if you re-apply for a new one? No. It is because the IMSI number corresponding to your mobile phone number has been modified by the mobile operator to the IMSI number of the new SIM card. 
    So why can some mobile phone numbers be displayed? 
    This is like a variable. When the mobile operator assigns a value to it, it will naturally have a value. No assignment is naturally empty. 
For mobile users, the mobile phone number (MDN) is stored in the operator's server instead of the SIM card. The SIM card only keeps the IMSI and some verification information. Every time the mobile phone registers on the network, it will upload the IMSI and verification information to the operator's server in the form of SMS. After the server completes the registration action, it will send the registration result to the mobile phone in the form of SMS. The content delivered will vary depending on the conditions. 
    If the text message sent by the server does not contain the phone number, the phone cannot obtain the phone number. If the SMS contains the number, the mobile phone will cache it for other use. In addition, for the SIM card or UIM card of other operators, MDN may be stored in the UIM card. It is unlikely that 100% of the phone number can be obtained. 
    Mobile Shenzhouxing, Unicom's card can be obtained. The dynamic zone can not be obtained. Other cards have not been tried.  If you
    can read the SIM card number, there should be a premise. That is, the SIM card has written the local number, otherwise it is Unreadable.


ICCID
ICCID: Integrate circuit card identity Integrated circuit card identification code (solidified in the SIM card of the mobile phone) ICCID is the unique identification number of the IC card, consisting of 20 digits, and its coding format is: XXXXXX 0MFSS YYGXX XXXXX. on the back of the SIM card.


The first six operator codes: China Mobile's: 898600; 898602, China Unicom's: 898601, China Telecom's 898603


tool class to obtain mobile phone information
public class PhoneInfoUtils {
    private static String TAG = "PhoneInfoUtils";
    private TelephonyManager telephonyManager;
    / /Mobile operator number
    private String NetworkOperator;
    private Context context;
    public PhoneInfoUtils(Context context) {
        this.context = context;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    /**
     * Get sim card Iccid
     * /
    public String getIccid() {
        return telephonyManager.getSimSerialNumber();
    }
    /**
     * Get the phone number
     *
     * @return
     */
    public String getNativePhoneNumber() {
        return telephonyManager.getLine1Number();
    }
    /**
     * Get the phone service provider information
     *
     * @return
     */
    public String getProvidersName() {
        String providersName = "N/A";
        NetworkOperator = telephonyManager.getNetworkOperator();
        // The first 3 digits of 460 in the IMSI number are the country, and the next 2 digits of 00 and 02 are China Mobile , 01 is China Unicom, 03 is China Telecom.
        if (NetworkOperator.equals("46000") || NetworkOperator.equals("46002")) {
            providersName = "China Mobile";//China Mobile
        } else if (NetworkOperator.equals("46001")) {
            providersName = " China Unicom";//China Unicom
        } else if (NetworkOperator.equals("46003")) {
            providersName = "China Telecom";//China Telecom
        }
        return providersName;
    }
    /**
     * Get mobile phone IMEI
     *
     * @return
     * /
    public String getIMEI() {
        return telephonyManager.getDeviceId();
    }
    /**
     * Get the phone IMSI
     */
    public String getIMSI(Context context) {
        return telephonyManager.getSubscriberId();
    }
    public String getPhoneInfo() {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        StringBuffer sb = new StringBuffer();
        sb.append("\nLine1Number = " + tm.getLine1Number());
        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());//移动运营商编号
        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());//移动运营商名称
        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
        sb.append("\nSimOperator = " + tm.getSimOperator());
        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
        sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
        sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId ());
        return sb.toString();
    }
    //Get the real physical address of the machine
    public String getLocalMacAddress() {
        String macAddress = Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
        return macAddress;
    }
}
If you can't get the local number through phoneInfoUtils.getNativePhoneNumber(), you need to use a roundabout way to get it.
Here is a solution: send a short message to the service provider, and extract the local number from the returned information. Use the
mobile card as the For example, send BJ to 10086, and the returned information includes the number of the machine. Tool class for


sending
SMS: public class SMSUtils {
    // 正则提取手机号码
    public static String GetPhoneNumberFromSMSText(String sms) {
        String phoneNumberRegex = "\\d*";
        Pattern p = Pattern.compile(phoneNumberRegex);
        Matcher m = p.matcher(sms);
        while (m.find()) {
            String msg = m.group();
            if (!TextUtils.isEmpty(msg) && msg.length() == 11) {
                return msg;
            }
        }
        return "";
    }
    // 发送短信
    public void SendSMS(Context context, String number, String text) {
        PendingIntent pi = PendingIntent.getActivity(context, 0,
                new Intent(context, context.getClass()), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(number, null, text, pi, null);
    }
    // 发送短信
    public void SendSMS2(Context context, String number, String text) {
        String SENT = "sms_sent";
        String DELIVERED = "sms_delivered";
        PendingIntent sentPI = PendingIntent.getActivity(context, 0, new Intent(SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getActivity(context, 0, new Intent(DELIVERED), 0);
        SmsManager smsm = SmsManager.getDefault();
        smsm.sendTextMessage(number, null, text, sentPI, deliveredPI);
    }
}
写一个短信广播监听:
public class SMSReceiver extends BroadcastReceiver {
    public static final String GetNumberAddress = "10086";// 短信来源
    public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    public static String phoneNumber = "";
    private Handler handler;
    public SMSReceiver(Handler handler){
        this.handler = handler;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (SMS_RECEIVED.equals(intent.getAction())) {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] message = new SmsMessage[pdus.length];
            StringBuilder sb = new StringBuilder();
            String address = "";
            for (int i = 0; i < pdus.length; i++) { // Splicing message strings
                // Although it is a loop, the length of pdus is generally 1
                message [i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                sb.append("Received SMS from:\n");
                address = message[i].getDisplayOriginatingAddress();
                sb.append(address + "\n");
                sb.append("Content:" + message[i].getDisplayMessageBody());
            }
            if (address.equals(GetNumberAddress)) { // Add phoneNumber to the message queue and let the main Activity handle
                phoneNumber = SMSUtils.GetPhoneNumberFromSMSText(sb.toString());
                Message messagel = handler.obtainMessage();
                messagel.what = MainActivity.HANDLER_REV_MSG;
                messagel.obj = phoneNumber;
                handler.handleMessage(messagel);
            }
        }
    }
}
Then call where you need to send SMS:
//Send SMS
SMSUtils smscore = new SMSUtils();
smscore.SendSMS2(this, "10086", "BJ");
Process the received SMS message in the broadcast monitor.
Registered broadcast receivers remember to destroy them in onDestory.
@Override
protected void onDestroy() {
    super.onDestroy();
    if (smsReceiver != null) {
        unregisterReceiver(smsReceiver);
    }
    ... ...
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324753953&siteId=291194637