Android phone to get phone number implementation method _android

Source: Internet
Author: User

Recently do a project, need to get mobile phone number. The mobile card used in the project is a telecom card, so take this as an example.

The method of not meeting the requirement on the Internet

Google a bit, the online practice is as follows:

Get cell phone number 
Telephonymanager TM = (Telephonymanager) this.getsystemservice (context.telephony_service); 
 String DeviceID = Tm.getdeviceid ()//Get Smart Device unique number 
 string te1 = Tm.getline1number ();//Get Native number 
 string IMEI = Tm.getsimserialnumber ()//Get the serial number of the SIM card 
 

To add a permission:

<uses-permissionandroid:nameuses-permissionandroid:name= "Android.permission.READ_PHONE_STATE"/

It is possible to obtain the SIM card number, but at least it is not available on the mobile phone card.

The reason seems to be:

Mobile phone numbers are not all available. Just a part of it can be got. This is because the mobile operator did not write the phone number data to the SIM card. SIM card has only a unique number, for network and device identification that is the IMSI number, the cell phone signal can also be said through this number in the network transmission, not mobile phone number. Just imagine, after your SIM card is lost, a new one will change the number? That's because the IMSI number that corresponds to your mobile phone number is modified to the IMSI number of the new SIM card in the mobile operator.

So how do we solve this problem?

Bad implementation? One reason to fix you: the project needs!!

Check the basic information of the cell phone that column, also did not find a cell phone number, it seems that the phone does not seem to store the phone number. Then I have to use a flexible approach to get the phone number.

is to send a message to 10001, look for a bit, send a 501 query package situation can be. 10001 will give you a feedback containing your cell phone number.

Then we send a text message, then listen to get this text, and then decompose the string so that we can get the cell phone number.

If it is unicom or mobile should also have a similar situation. Besides texting them should not be charged ... O (∩_∩) o~

How is the program implemented?

1. Listening to SMS Class

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"; @Override public void OnReceive (context context, Intent Intent) {//TODO auto-generated Method stub if (intent.get Action (). Equals ("Android.provider.Telephony.SMS_RECEIVED")) {object[] pdus= (object[)) Intent.getextras (). Get ("PDU 
   S "); 
   Do not know why there is only one message, passed over is an array, perhaps in order to deal with the same time with the same number of milliseconds received multiple SMS//But this probability a little smsmessage[] message=new smsmessage[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, in fact 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 Operation class

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 android.app.Activity; 
Import android.app.PendingIntent; 
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 SMS public static St 
   Ring 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 (M.group ())) List.add (M.group ()); 
  } return list; }//===========================================================//////=============send SMS============= = = = public void Sendsms (String number,string text,context context) {pendingintent pi = Pendingintent.getactivi 
   Ty (context, 0, new Intent (Context, Context.getclass ()), 0); 
   Smsmanager SMS = Smsmanager.getdefault (); 
 
  Sms.sendtextmessage (number, null, text, PI, null); 
   The 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 cont Ext, 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. Pop-up 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 ShowDialog (context context, String text) { 
  Alertdialog.builder Builder = new Builder (context); 
   Builder.setmessage (text); 
 
   Builder.settitle ("hint"); 
 
   Builder.setpositivebutton ("Confirm", 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 = "Lab.sodino.sms.delivery"; 
 private static final String Action_sms_receiver = "Android.provider.Telephony.SMS_RECEIVED"; 
 

Register to receive downlink Receiver 
  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); 

Run Result:

We can get the phone number we want!

The above is Android to get the code to achieve the mobile phone number, I hope to help friends in need, thank you for your support of this site!

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: info-contact@alibabacloud.com 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.