Sample code for Android app to get mobile device information and mobile phone number
Source: Internet
Author: User
<span id="Label3"></p><p><p>Below I from the perspective of Android development, simply write how to get mobile device information and mobile phone number</p></p><p><p></p></p><p><p>Preparation Conditions: an Android phone, mobile SIM card to ensure plug-in phone, Eclipse ADT and ANDROID-SDK development environment</p></p><p><p></p></p><p><p>First Step: Create a new Android project (jinshantest),</p></p><p><p></p></p><p><p></p></p><p><p>and need to add permissions in the Project's androidmanifest.xml file</p></p><p><p><uses-permission android:name= "android.permission.READ_PHONE_STATE"/></p></p><p><p></p></p><p><p>Legend:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Step Two: Create a new tool class Phoneinfo.java</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View Plaincopy <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Package com.jinshan.test;</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Import android.content.Context;</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Import android.telephony.TelephonyManager;</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">/**</span></li></li> <li><li><span class="comment">* Read the phone device information test code</span></li></li> <li class="alt"><li class="alt"><span class="comment">* Http://www.souapp.com Search Application Network</span></li></li> <li><li><span class="comment">* [email protected]</span></li></li> <li class="alt"><li class="alt"><span class="comment">* Song Lipo</span></li></li> <li><li><span class="comment">*/</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">class Phoneinfo {</span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Private Telephonymanager telephonymanager; </span></li></li> <li><li><span class="comment">/** </span></li></li> <li class="alt"><li class="alt"><span class="comment">* International Mobile Subscriber Identification code</span></li></li> <li><li><span class="comment">*/</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Private String IMSI; </span></li></li> <li><li><span class="keyword">Private Context cxt; </span></li></li> <li class="alt"><li class="alt">public <span class="keyword">phoneinfo (context Context) {</span></li></li> <li><li>cxt=context;</li></li> <li class="alt"><li class="alt">Telephonymanager = (telephonymanager) Context</li></li> <li><li>. Getsystemservice (context.telephony_service);</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">/** </span></li></li> <li><li><span class="comment">* Get Phone number</span></li></li> <li class="alt"><li class="alt"><span class="comment">*/</span></li></li> <li><li>public <span class="keyword">String getnativephonenumber () {</span></li></li> <li class="alt"><li class="alt">String nativephonenumber=<span class="keyword">null; </span></li></li> <li><li>Nativephonenumber=telephonymanager.getline1number ();</li></li> <li class="alt"><li class="alt"><span class="keyword">return nativephonenumber; </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">/** </span></li></li> <li class="alt"><li class="alt"><span class="comment">* Access to mobile service provider information</span></li></li> <li><li><span class="comment">*/</span></li></li> <li class="alt"><li class="alt">public <span class="keyword">String getprovidersname () {</span></li></li> <li><li>String providersname = <span class="string">"n/a"; </span></li></li> <li class="alt"><li class="alt"><span class="keyword">try{</span></li></li> <li><li>IMSI = Telephonymanager.getsubscriberid ();</li></li> <li class="alt"><li class="alt"><span class="comment">//imsi Front 3 bit 460 is the country, followed by 2 bit 00 02 is China mobile, 01 is China unicom, 03 is Chinese telecom. </span></li></li> <li><li>System.out.println (IMSI);</li></li> <li class="alt"><li class="alt"><span class="keyword">if (imsi.startswith (<span class="string">"46000") | | Imsi.startswith (<span class="string">"46002")) {</span> </span> </span></li></li> <li><li>Providersname = <span class="string">"china mobile"; </span></li></li> <li class="alt"><li class="alt">} <span class="keyword">Else <span class="keyword">if (imsi.startswith (<span class="string">"46001")) {</span> </span> </span></li></li> <li><li>Providersname = <span class="string">"china unicom"; </span></li></li> <li class="alt"><li class="alt">} <span class="keyword">Else <span class="keyword">if (imsi.startswith (<span class="string">"46003")) {</span> </span> </span></li></li> <li><li>Providersname = <span class="string">"china telecom"; </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}<span class="keyword">catch (Exception e) {</span></li></li> <li class="alt"><li class="alt">E.printstacktrace ();</li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">return providersname; </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>public <span class="keyword">String getphoneinfo () {</span></li></li> <li class="alt"><li class="alt">Telephonymanager TM = (telephonymanager) Cxt.getsystemservice (context.telephony_service);</li></li> <li><li>StringBuilder sb = <span class="keyword">new StringBuilder (); </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li>Sb.append (<span class="string">"\ndeviceid (IMEI) =" + Tm.getdeviceid ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\ndevicesoftwareversion =" + tm.getdevicesoftwareversion ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nline1number =" + tm.getline1number ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nnetworkcountryiso =" + tm.getnetworkcountryiso ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nnetworkoperator =" + tm.getnetworkoperator ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nnetworkoperatorname =" + tm.getnetworkoperatorname ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nnetworktype =" + tm.getnetworktype ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nphonetype =" + tm.getphonetype ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nsimcountryiso =" + tm.getsimcountryiso ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nsimoperator =" + tm.getsimoperator ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nsimoperatorname =" + tm.getsimoperatorname ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nsimserialnumber =" + tm.getsimserialnumber ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nsimstate =" + tm.getsimstate ()); </span></li></li> <li class="alt"><li class="alt">Sb.append (<span class="string">"\nsubscriberid (IMSI) =" + tm.getsubscriberid ()); </span></li></li> <li><li>Sb.append (<span class="string">"\nvoicemailnumber =" + tm.getvoicemailnumber ()); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">return sb.tostring (); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p><br><br></p></p><p><p></p></p><p><p>Step three: Add the calling code in the activity,jinshantestactivity that starts:</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View Plaincopy <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Package com.jinshan.test;</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Import android.app.Activity;</span></li></li> <li><li><span class="keyword">Import android.os.Bundle;</span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Public <span class="keyword">class Jinshantestactivity <span class="keyword">extends Activity {</span> </span></span></li></li> <li class="alt"><li class="alt"><span class="comment">/** Called when the activity is first Created. *</span> /</li></li> <li><li><span class="annotation">@Override</span></li></li> <li class="alt"><li class="alt"><span class="keyword">public <span class="keyword">void OnCreate (Bundle Savedinstancestate) {</span> </span></li></li> <li><li><span class="keyword">super.oncreate (savedinstancestate); </span></li></li> <li class="alt"><li class="alt">Setcontentview (r.layout.main);</li></li> <li><li></li></li> <li class="alt"><li class="alt">Phoneinfo siminfo = <span class="keyword">new Phoneinfo (jinshantestactivity. <span class="keyword"> this); </span></span></li></li> <li><li>System.out.println (<span class="string">"getprovidersname:" +siminfo.getprovidersname ()); </span></li></li> <li class="alt"><li class="alt">System.out.println (<span class="string">"getnativephonenumber:" +siminfo.getnativephonenumber ()); </span></li></li> <li><li>System.out.println (<span class="string">"------------------------"); </span></li></li> <li class="alt"><li class="alt">System.out.println (<span class="string">"getphoneinfo:" +siminfo.getphoneinfo ()); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li></li></li> <li class="alt"><li class="alt"></li></li> <li><li>}</li></li> </ol><p><p><br>Fourth step, connect your phone to your computer using a USB cable, and make sure it's connected and start working on the project</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>finally, We look at the Logcat log and the results are as follows:</p></p><p><p>------------------------------------------------------------</p></p><p>04-01 16:20:57.105:i/system.out (952): 460003121934674<br>04-01 16:20:57.105:i/system.out (952): getprovidersname: China Mobile<br>04-01 16:20:57.115:i/system.out (952): getnativephonenumber:136xxxxxxx<br>04-01 16:20:57.115:i/system.out (952):------------------------<br>04-01 16:20:57.145:i/system.out (952): getphoneinfo:<br>04-01 16:20:57.145:i/system.out (952): DeviceId (IMEI) = 352xxxxxxxx61328<br>04-01 16:20:57.145:i/system.out (952): devicesoftwareversion = 01<br>04-01 16:20:57.145:i/system.out (952): line1number = 136XXXXXXX<br>04-01 16:20:57.145:i/system.out (952): Networkcountryiso = cn<br>04-01 16:20:57.145:i/system.out (952): networkoperator = 46000<br>04-01 16:20:57.145:i/system.out (952): networkoperatorname = China Mobile<br>04-01 16:20:57.145:i/system.out (952): Networktype = 2<br>04-01 16:20:57.145:i/system.out (952): phonetype = 1<br>04-01 16:20:57.145:i/system.out (952): Simcountryiso = cn<br>04-01 16:20:57.145:i/system.out (952): simoperator = 46000<br>04-01 16:20:57.145:i/system.out (952): simoperatorname = CMCC<br>04-01 16:20:57.145:i/system.out (952): simserialnumber = 898xxxxxx90108<br>04-01 16:20:57.145:i/system.out (952): simstate = 5<br>04-01 16:20:57.145:i/system.out (952): SubscriberId (IMSI) = 46000xxxxxxxx4674</p><p><p>So let's explain what the above information means.</p></p><p><p></p></p><p><p>Getnativephonenumber Get the phone number</p></p><p><p>DeviceId (IMEI) Mobile International Mobile Subscriber ID</p></p><p><p>Networkoperator Mobile operator number</p></p><p><p>Networkoperatorname Mobile operator Name</p></p><p><p>Simserialnumber simoperator simcountryiso simserialnumber SubscriberId (IMSI) Some details about mobile SIM card</p></p><p><p></p></p><p><p>In fact, the code does not write to get system.android_id this operation, because many mobile devices do not get andnroid_id</p></p><p><p></p></p><p><p>of course, today we mainly talk about <uses-permission android:name= "android.permission.READ_PHONE_STATE"/> Light Use this permission we can get mobile device information and mobile phone number</p></p><p><p>If you want to get wifi, bluetooth, GPS, read-write sdcard more information, you need to add additional Permissions.</p></p><p><p></p></p><p><p>In addition, this test I take my phone SIM card test is able to get to the mobile phone number, of course, Some mobile phone number is not available, here also gives you a list of reasons,</p></p><p><p></p></p><p><p>Mobile phone numbers are not available for All. Just a part of it can get.</p></p><p><p>This is due to the fact that the mobile operator does not write data from the mobile phone number to the SIM Card. The SIM card only has the unique number, for the network and the device recognition that is the IMSI number, the cell phone signal also can say is passes through this number in the network, is not the mobile phone number. Just imagine, after your sim is lost, do you have a new one that will change the number? no, it is because the IMSI number that corresponds to your mobile phone number is modified in the mobile operator to the IMSI number of the new Sim Card.<br>So why does the phone number have to show it?<br>This is like a variable, and when the mobile operator assigns it a value, it will naturally have a value. No value is naturally Empty.<br>For mobile users, The mobile number (MDN) is saved in the Operator's server instead of being stored in the SIM Card. The SIM card retains only IMSI and some verification information. Every time the mobile network registration, will be in the form of text messages to the IMSI and authentication information to the Operator's server, the server after the completion of the registration action, will be in the form of text messages will be issued to the mobile PHONE. The content will be different depending on the Conditions.<br>If the server in the issued text message, does not include the phone number, the phone is unable to obtain the phone number. If the text contains a number, the phone will cache it for his use. in addition, for other operators ' SIM cards or Uim cards, the MDN may be saved in the Uim Card. 100% being able to get a native number is unlikely.<br>Mobile Shenzhou line, Unicom's card can be taken. the dynamic zone is not available. other cards have not been tried Yet.<br>The ability to read the SIM card number should have a precondition. that is, the SIM card has been written to the local number, otherwise it cannot be read.</p></p><p><p>Sample code for Android app to get mobile device information and mobile phone number</p></p></span>
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