Sample code for Android app to get mobile device information and mobile phone number

Source: Internet
Author: User



Below I from the perspective of Android development, simply write how to get mobile device information and mobile phone number






Preparation conditions: An Android phone, mobile SIM card to ensure plug-in phone, Eclipse ADT and ANDROID-SDK development environment






First step: Create a new Android project (Jinshantest),









and need to add permissions in the project's Androidmanifest.xml file



<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>






Legend:












Step Two: Create a new 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 mobile device information test code
 9. * http://www.souapp.com 搜 应用 网
 10. * [email protected]
 11. * Song Libo
 12. * /
 13.public class PhoneInfo {
 14.
 15. private TelephonyManager telephonyManager;
 16. / **
 17. * International Mobile Subscriber Identity
 18. * /
 19. private String IMSI;
 20. private Context cxt;
 21. public PhoneInfo (Context context) {
 22. cxt = context;
 23. telephonyManager = (TelephonyManager) context
 24. .getSystemService (Context.TELEPHONY_SERVICE);
 25.}
 26.
 27. / **
 28. * Get Phone Number
 29. * /
 30. public String getNativePhoneNumber () {
 31. String NativePhoneNumber = null;
 32. NativePhoneNumber = telephonyManager.getLine1Number ();
 33. return NativePhoneNumber;
 34.}
 35.
 36. / **
 37. * Get mobile phone service provider information
 38. * /
 39. public String getProvidersName () {
 40. String ProvidersName = "N / A";
 41. try {
 42. IMSI = telephonyManager.getSubscriberId ();
 43. // The first three digits of the IMSI number are 460, and the next two digits are 00, China Mobile, 02, China Unicom, and 03, 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 three: Add the calling code in the activity,jinshantestactivity that starts:





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.}  







Fourth step, connect your phone to your computer using a USB cable, and make sure it's connected and start working on the project















Finally, we 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:136xxxxxxx
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 = 136XXXXXXX
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


So let's explain what the above information means.






Getnativephonenumber Get the phone number



DeviceId (IMEI) Mobile International Mobile Subscriber ID



Networkoperator Mobile operator number



Networkoperatorname Mobile operator Name



Simserialnumber simoperator simcountryiso simserialnumber SubscriberId (IMSI) Some details about mobile SIM card






In fact, the code does not write to get system.android_id this operation, because many mobile devices do not get andnroid_id






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



If you want to get WiFi, Bluetooth, GPS, read-write sdcard more information, you need to add additional permissions.






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,






Mobile phone numbers are not available for all. Just a part of it can get.



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.
So why does the phone number have to show it?
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.
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.
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.
Mobile Shenzhou Line, Unicom's card can be taken. The dynamic zone is not available. Other cards have not been tried yet.
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.









Other great article articles




Android KSOAP2 call. NET Webservicejquery Tutorial (8)- Using the reverse Insert method for DOM tree operation Android Learning Note (34) using Alertdialog to create a simple dialog box Android learning Note (33) Gallery View (Gallery) features and usage Android navidgation drawer How to change the list selection in the navigation drawer ...





More about Android development articles




Sample code for Android app to get mobile device information and 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: 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.