Sample Code for android APP to obtain mobile phone device information and mobile phone numbers. android
From the android development perspective, I will briefly describe how to obtain the mobile phone device information and mobile phone number.
Prerequisites: an android phone and SIM card must be inserted into the mobile phone, eclipse ADT, and android-sdk development environment.
Step 1: Create an android Project (JinshanTest ),
Add permissions to the AndroidManifest. xml file of the project.
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
Legend:
Step 2: create a tool class PhoneInfo. java
1. package com. jinshan. test; 2. 3. 4. import android. content. context; 5. import android. telephony. telephonyManager; 6. 7. /** 8. * read the mobile phone device information test code 9. * http://www.souapp.com search application network 10. * song2c@163.com 11. * song Libo 12. */13. public class PhoneInfo {14. 15. private TelephonyManager telephonyManager; 16. /** 17. * International Mobile User Identification Code 18. */19. private String IMSI; 20. private Context cxt; 21. public PhoneInfo (Context co Ntext) {22. cxt = context; 23. telephonyManager = (TelephonyManager) context 24 .. getSystemService (Context. TELEPHONY_SERVICE); 25 .} 26. 27. /** 28. * Get the phone number 29. */30. public String getNativePhoneNumber () {31. string NativePhoneNumber = null; 32. nativePhoneNumber = telephonyManager. getLine1Number (); 33. return NativePhoneNumber; 34 .} 35. 36. /** 37. * getting mobile phone service provider Information 38. */39. public String getProviders Name () {40. string ProvidersName = "N/A"; 41. try {42. IMSI = telephonyManager. getSubscriberId (); 43. // The first three digits of IMSI number 460 are countries, followed by two digits 00 02 are China Mobile, 01 is China Unicom, and 03 is China Telecom. 44. system. out. println (IMSI); 45. if (IMSI. startsWith ("46000") | IMSI. startsWith ("46002") {46. providersName = "China Mobile"; 47 .} else if (IMSI. startsWith ("46001") {48. providersName = "China Unicom"; 49 .} else if (IMSI. startsWith ("46003") {50. providersName = "China Telecom"; 51 .} 52 .} catch (Exception e) {53. e. printStackTrace (); 54 .} 55. return ProvidersName; 56 .} 57. 58. public String getPhoneInfo () {59. telephonyManager tm = (TelephonyManager) cxt. getSystemService (Context. TELEPHONY_SERVICE); 60. stringBuilder sb = new StringBuilder (); 61. 62. sb. append ("\ nDeviceId (IMEI) =" + tm. getDeviceId (); 63. sb. append ("\ nDeviceSoftwareVersion =" + tm. getDeviceSoftwareVersion (); 64. sb. append ("\ nLine1Number =" + tm. getLine1Number (); 65. sb. append ("\ nNetworkCountryIso =" + tm. getNetworkCountryIso (); 66. sb. append ("\ nNetworkOperator =" + tm. getNetworkOperator (); 67. sb. append ("\ nNetworkOperatorName =" + tm. getNetworkOperatorName (); 68. sb. append ("\ nNetworkType =" + tm. getNetworkType (); 69. sb. append ("\ nPhoneType =" + tm. getPhoneType (); 70. sb. append ("\ nSimCountryIso =" + tm. getSimCountryIso (); 71. sb. append ("\ nSimOperator =" + tm. getSimOperator (); 72. sb. append ("\ nSimOperatorName =" + tm. getSimOperatorName (); 73. sb. append ("\ nSimSerialNumber =" + tm. getSimSerialNumber (); 74. sb. append ("\ nSimState =" + tm. getSimState (); 75. sb. append ("\ nSubscriberId (IMSI) =" + tm. getSubscriberId (); 76. sb. append ("\ nVoiceMailNumber =" + tm. getVoiceMailNumber (); 77. return sb. toString (); 78 .} 79 .}
Step 3: Add the following code to the started Activity and JinshanTestActivity:
1.package com.jinshan.test; 2. 3.import android.app.Activity; 4.import android.os.Bundle; 5. 6.public class JinshanTestActivity extends Activity { 7. /** Called when the activity is first created. */ 8. @Override 9. public void onCreate(Bundle savedInstanceState) { 10. super.onCreate(savedInstanceState); 11. setContentView(R.layout.main); 12. 13. PhoneInfo siminfo = new PhoneInfo(JinshanTestActivity.this); 14. System.out.println("getProvidersName:"+siminfo.getProvidersName()); 15. System.out.println("getNativePhoneNumber:"+siminfo.getNativePhoneNumber()); 16. System.out.println("------------------------"); 17. System.out.println("getPhoneInfo:"+siminfo.getPhoneInfo()); 18. } 19. 20. 21. 22.}
Step 4: connect the mobile phone to the computer using USB cable to ensure connectivity and start running the project
Finally, let's look at the LOGCAT log and the results are as follows:
------------------------------------------------------------
04-01 16:20:57. 105: I/System. out (952): 460003121934674
04-01 16:20:57. 105: I/System. out (952): getProvidersName: China Mobile
04-01 16:20:57. 115: I/System. out (952): getNativePhoneNumber: 136 XXXXXXX
04-01 16:20:57. 115: I/System. out (952 ):------------------------
04-01 16:20:57. 145: I/System. out (952): getPhoneInfo:
04-01 16:20:57. 145: I/System. out (952): DeviceId (IMEI) = 352XXXXXXXX61328
04-01 16:20:57. 145: I/System. out (952): DeviceSoftwareVersion = 01
04-01 16:20:57. 145: I/System. out (952): Line1Number = 136 XXXXXXX
04-01 16:20:57. 145: I/System. out (952): NetworkCountryIso = cn
04-01 16:20:57. 145: I/System. out (952): NetworkOperator = 46000
04-01 16:20:57. 145: I/System. out (952): NetworkOperatorName = China Mobile
04-01 16:20:57. 145: I/System. out (952): NetworkType = 2
04-01 16:20:57. 145: I/System. out (952): PhoneType = 1
04-01 16:20:57. 145: I/System. out (952): SimCountryIso = cn
04-01 16:20:57. 145: I/System. out (952): SimOperator = 46000
04-01 16:20:57. 145: I/System. out (952): SimOperatorName = CMCC
04-01 16:20:57. 145: I/System. out (952): SimSerialNumber = 898xxxxxx90108
04-01 16:20:57. 145: I/System. out (952): SimState = 5
04-01 16:20:57. 145: I/System. out (952): SubscriberId (IMSI) = 46000xxxxxxxx4674
Let's explain what the above information actually means.
Mobile phone number obtained by getNativePhoneNumber
DeviceId (IMEI) Mobile Phone International Mobile User Identification Code
NetworkOperator mobile operator no.
NetworkOperatorName Mobile Operator name
SimSerialNumber SimOperator SimCountryIso SimSerialNumber SubscriberId (IMSI)
In fact, the operation System. ANDROID_ID is not written in the code, because many mobile devices cannot obtain andnroid_id.
Of course, today we will mainly talk about the mobile phone device information and mobile phone number that we can obtain by using this permission. <uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
If you want to obtain more information about WIFI, Bluetooth, GPS, and read/write SDCARD, you need to add other permissions.
In addition, in this test, I can obtain the mobile phone number from my mobile phone SIM card. Of course, some mobile phone numbers cannot be obtained. The reasons are also listed here,
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 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.
Why can I display some mobile phone numbers?
This is like a variable. When a mobile operator assigns a value to it, it naturally has a value. Null if no value is assigned.
For mobile users, the mobile phone number (MDN) is stored on the carrier's server instead of the SIM card. The SIM card only retains IMSI and some verification information. Each time a mobile phone is registered, it uploads IMSI and verification information to the carrier's server via text message. After the server completes the registration, the registration result will be sent to the mobile phone via text message. The content delivered varies according to different conditions.
If the server sends a text message that does not contain the phone number, the phone number cannot be obtained. If the phone number is included in the text message, the phone will cache it for him to use. In addition, for SIM cards or uimcards of other operators, MDN may be stored in the uimcard. 100% it is unlikely that a local number can be obtained.
China Unicom's card can be obtained from China Mobile shenzhouxing. The motion zone cannot be obtained. Other cards have not been tried yet.
The premise is that the SIM card number can be read, that is, the SIM card has been written into the local number, otherwise it cannot be read.
Other brilliant articles
Android KSOAP2 call. net webservicejQuery tutorial (8)-DOM tree operation using reverse Insertion Method android learning notes (34) using AlertDialog to create a simple dialog box android learning notes (33) Gallery view (Gallery) android navidgation drawer in the navigation drawer how to change the List of selected items...