Android mobile phone number Retrieval

Source: Internet
Author: User

You need to obtain the mobile phone number for a recent project. The mobile phone card used in the project is a telecom card.

Methods that do not meet the requirements on the Internet


// Obtain the mobile phone number TelephonyManager tm = (TelephonyManager) this. getSystemService (Context. TELEPHONY_SERVICE); String deviceid = tm. getDeviceId (); // obtain the unique ID of a smart device, String te1 = tm. getLine1Number (); // obtain the local number String imei = tm. getSimSerialNumber (); // obtain the serial number of the SIM card String imsi = tm. getSubscriberId (); // get the user ID

Add permission:


The above method may obtain the SIM card number, but it cannot be obtained at least on the mobile phone card of China Telecom.

The reason seems to be:

Not all mobile phone numbers can be obtained. Only some of them can be obtained. This is because the mobile operator did not write the data of the mobile phone number into the SIM card. the SIM card only has a unique ID for the network and device to identify, that is, the IMSI number. The mobile phone signal can also be said to be transmitted through this number in the network, not the mobile phone number. Imagine if your SIM card is lost, will a new one be replaced? No, it is because the IMSI number corresponding to your mobile phone number is changed to the IMSI number of the new SIM card in the mobile operator.

So how can we solve this problem?

Poor implementation? One reason to deal with you: project needs !!

I checked the basic information of the mobile phone and did not find the mobile phone number. It seems that the mobile phone number does not exist. Then I only need to use a work und to get the mobile phone number.

It is to send a text message to 10001, find it, and send a 501 query package. 10001 will give you a feedback, including the mobile phone number

Then we send the text message, listen to it, and break down the string to get the mobile phone number.

Similar situations should also apply to China Unicom or China Mobile. What's more, text messages to them are free of charge... O (distinct _ distinct) O ~

How is the program implemented?

1. Listener SMS

Package com. example. smsmanager; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsMessage; public class SMS_Receiver extends BroadcastReceiver {final String GetNumberAddress = "10001"; @ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stubif (intent. getAction (). equals ("android. provider. telephony. SMS_RECEIVED ") {Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); // I don't know why there is only one message, but it is an array, it may be to process messages received in the same minute, in the same second, or in the same millisecond. // However, this probability is small: SmsMessage [] message = new smessage [pdus. length]; StringBuilder sb = new StringBuilder (); System. out. println ("pdus length" + pdus. length); String address = ""; for (int I = 0; I <pdus. length; I ++) {// although it is a loop, the pdus length 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 ();} System. out. println (sb. toString (); if (SMSCore. phoneNumber = "" & address. equals (GetNumberAddress) {SMSCore. phoneNumber = SMSCore. getPhoneNumberFromSMSText (sb. toString (); MessageTools. showDialog (context, address);} MessageTools. showDialog (context, sb. toString (). trim (); MessageTools. showDialog (context, SMSCore. phoneNumber );}}}

2. SMS operations

package com.example.smsmanager;import java.sql.Date;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.view.View;import android.view.View.OnClickListener;import android.content.BroadcastReceiver;import;import;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.telephony.SmsManager;import android.util.Log;import android.content.ContextWrapper;public class SMSCore {public static String PhoneNumber="";//==============Get Phone Number ==============================    //get the phone number from smspublic static String GetPhoneNumberFromSMSText(String sms){List<String> list=GetNumberInString(sms);for(String str:list){if(str.length()==11)return str;}return "";}public static List<String> GetNumberInString(String str){List<String> list=new ArrayList<String>();        String regex = "\\d*";                        Pattern p = Pattern.compile(regex);                        Matcher m = p.matcher(str);                        while (m.find()) {                        if (!"".equals(                         list.add(;                          }                      return list;}//===========================================================//////=============Send SMS================public  void SendSMS(String number,String text,Context context){ 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(String number,String text,Context context){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);//////// registerReceiver(new BroadcastReceiver(){//        @Override//        public void onReceive(Context context, Intent intent){//            switch(getResultCode())//            {//                case Activity.RESULT_OK://                Log.i("====>", "RESULT_OK");//                    System.out.println("RESULT_OK");//                    //                    break;//                case Activity.RESULT_CANCELED://                    Log.i("=====>", "RESULT_CANCELED");//                    break;//            }//        }//    }, new IntentFilter(DELIVERED));//  //SmsManager smsm = SmsManager.getDefault();smsm.sendTextMessage(number, null, text, sentPI, deliveredPI);}//=====================================}

3. Dialog Box Operation class

Package com. example. smsmanager; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. context; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; public class MessageTools {public static void showdiener (Context context, String text) {AlertDialog. builder builder = new Builder (context); builder. setMessage (text); builder. setTitle ("prompt"); builder. setPositiveButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. dismiss (); dialog. dismiss () ;}}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); builder. create (). show ();}}

4. The call is as follows:

//    private static final String ACTION_SMS_SEND = "lab.sodino.sms.send";      private static final String ACTION_SMS_DELIVERY = "";      private static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";

// Register and receive downstream referers SMS_Receiver smsReceiver = new SMS_Receiver (); IntentFilter receiverFilter = new IntentFilter (ACTION_SMS_RECEIVER); registerReceiver (smsReceiver, receiverFilter ); // send SMS SMSCore smscore = new SMSCore (); smscore. sendSMS2 ("10001", "501", context );

Running result:

We can get the desired mobile phone number!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.