Zhao yazhi _ android obtains the operator of the local device, the mobile phone number can be obtained, and Zhao yazhi _ android

Source: Internet
Author: User

Zhao yazhi _ android obtains the operator of the local device, the mobile phone number can be obtained, and Zhao yazhi _ android

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.

The SIM card in China stores the imsi number, and no phone number is stored on the machine. Only when you contact the base station will the carrier assign you a number.


But I also wrote a class implementation to get the carrier

Layout file:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: gravity = "center" android: orientation = "vertical"> <TextView android: id = "@ + id/TV _num" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <TextView android: id = "@ + id/TV _privoid" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/getSIMInfo" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "Get mobile phone number and other information"> </Button> </LinearLayout>


SIMCardInfo. java:

/*** @ Author yazhizhao * 20142014-10-22 9:13:04 */package com. example. android_sms; import android. content. context; import android. telephony. telephonyManager; import android. util. log;/*** @ author yazhizhao 9:13:04 */public class SIMCardInfo {/*** TelephonyManager provides the entry for obtaining Communication Service Information on the device. Applications can use this method to determine the access information of telecom service providers and countries and some types of users. * The application can also register a listener to change the phone receiving status. You do not need to directly instantiate this class * use Context. getSystemService (Context. TELEPHONY_SERVICE) to obtain the instance of this class. */Private TelephonyManager telephonyManager; private String IMSI; // International Mobile User identification code public SIMCardInfo (Context context) {telephonyManager = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE);}/*** get the current phone number ** @ author yazhizhao 20142014-10-22 9:44:19 * @ return */public String getNativePhoneNumber () {String NativePhoneNumber = null; nativePhoneNumber = telephonyManager. getLine1Numbe R (); return NativePhoneNumber;}/*** the permission <uses-permission * android: name = "android. permission. READ_PHONE_STATE "/> ** @ author yazhizhao 9:44:31 * @ return */public String getProvidersName () {String ProvidersName = null; // return a unique user ID; the ID of this card, IMSI = telephonyManager. getSubscriberId (); // the first three digits of IMSI are countries, followed by two digits: 00 02: China Mobile, 01: China Unicom, and 03: China Telecom. Log. I ("MainActivity", "IMSI" + IMSI); if (IMSI. startsWith ("46000") | IMSI. startsWith ("46002") {ProvidersName = "China Mobile";} else if (IMSI. startsWith ("46001") {ProvidersName = "China Unicom";} else if (IMSI. startsWith ("46003") {ProvidersName = "China Telecom";} return ProvidersName ;}}


MainActivity. java

Package com. example. android_sms; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. textView;/*** pay attention to the permission <uses-permission android: name = "android. permission. READ_PHONE_STATE "/> ** @ author yazhizhao 20142014-10-22 9:18:04 */public class MainActivity extends Activity {private TextView number; private TextView privoid; private String TAG =" MainActivity "; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); number = (TextView) this. findViewById (R. id. TV _num); privoid = (TextView) this. findViewById (R. id. TV _privoid);} public void click (View v) {SIMCardInfo siminfo = new SIMCardInfo (MainActivity. this); Log. I (TAG, "carrier" + siminfo. getProvidersName (); Log. I (TAG, "phone" + siminfo. getNativePhoneNumber (); number. setText (siminfo. getNativePhoneNumber (); privoid. setText (siminfo. getProvidersName ());}}

Running effect:







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.