在AndroidManifest.xml文件中增加
<!--允許讀取電話狀態SIM的權限-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
代碼如下
TelephonyManager telephonyManager =
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//手機串號:GSM手機的IMEI 和 CDMA手機的 MEID.
String deviceID = telephonyManager.getDeviceId();
//手機號(有些手機號無法獲取,是因爲運營商在SIM中沒有寫入手機號)
String tel = telephonyManager.getLine1Number();
//獲取手機SIM卡的序列號
/**
* Returns the serial number of the SIM, if applicable. Return null if it is
* unavailable.
* <p>
* Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
*/
String simSerialNumber = telephonyManager.getSimSerialNumber();
/**
* Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
* Return null if it is unavailable.
* <p>
* Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
*/
//獲取客戶id,在gsm中是imsi號
String imsi = telephonyManager.getSubscriberId();
/**
* Returns the current location of the device.
*<p>
* If there is only one radio in the device and that radio has an LTE connection,
* this method will return null. The implementation must not to try add LTE
* identifiers into the existing cdma/gsm classes.
*<p>
* In the future this call will be deprecated.
*<p>
* @return Current location of the device or null if not available.
*
* <p>Requires Permission:
* {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or
* {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION}.
*/
//電話方位
CellLocation str = telephonyManager.getCellLocation();
/**
* Returns the alphabetic name of current registered operator.
* <p>
* Availability: Only when user is registered to a network. Result may be
* unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
* on a CDMA network).
*/
//運營商名稱,注意:僅當用戶已在網絡註冊時有效,在CDMA網絡中結果也許不可靠
String networkoperatorName = telephonyManager.getNetworkOperatorName();
/**
* Retrieves the alphabetic identifier associated with the voice
* mail number.
* <p>
* Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
*/
//取得和語音郵件相關的標籤,即爲識別符
String voiceMail = telephonyManager.getVoiceMailAlphaTag();
/**
* Returns the voice mail number. Return null if it is unavailable.
* <p>
* Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
*/
//獲取語音郵件號碼:
String voiceMailNumber = telephonyManager.getVoiceMailNumber();
/**
* Returns the ISO country code equivalent for the SIM provider's country code.
*/
//獲取ISO國家碼,相當於提供SIM卡的國家碼。
String simCountryIso = telephonyManager.getSimCountryIso();
/**
* Returns one of the following constants that represents the current state of all
* phone calls.
*
* {@link TelephonyManager#CALL_STATE_RINGING}響鈴
* {@link TelephonyManager#CALL_STATE_OFFHOOK}摘機(佔用)狀態
* {@link TelephonyManager#CALL_STATE_IDLE}無活動
*/
int callState = telephonyManager.getCallState();
/**
* 設備的軟件版本號:
* Returns the software version number for the device, for example,
* the IMEI/SV for GSM phones. Return null if the software version is
* not available.
*
* <p>Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
*/
String devicesoftware = telephonyManager.getDeviceSoftwareVersion();
/**
* Returns the ISO country code equivalent of the current registered
* operator's MCC (Mobile Country Code).獲取ISO標準的國家碼,即國際長途區號。
* <p>
* Availability: Only when user is registered to a network. Result may be
* unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
* on a CDMA network).
*/
String networkCountry = telephonyManager.getNetworkCountryIso();
/**
* Returns the numeric name (MCC+MNC) of current registered operator.
* <p>(mobile country code + mobile network code)
* Availability: Only when user is registered to a network. Result may be
* unreliable on CDMA networks (use {@link #getPhoneType()} to determine if
* on a CDMA network).
*/
String networkoperator = telephonyManager.getNetworkOperator();
/**
* @return the NETWORK_TYPE_xxxx for current data connection.
* 當前使用的網絡類型:
* 例如: NETWORK_TYPE_UNKNOWN 網絡類型未知 0
* NETWORK_TYPE_GPRS GPRS網絡 1
* NETWORK_TYPE_EDGE EDGE網絡 2
* NETWORK_TYPE_UMTS UMTS網絡 3
* NETWORK_TYPE_HSDPA HSDPA網絡 8
* NETWORK_TYPE_HSUPA HSUPA網絡 9
*/
int netWorkType = telephonyManager.getNetworkType();
/**
* Returns a constant indicating the device phone type. This
* indicates the type of radio used to transmit voice calls.
*
* @see #PHONE_TYPE_NONE
* @see #PHONE_TYPE_GSM
* @see #PHONE_TYPE_CDMA
* @see #PHONE_TYPE_SIP
*/
int phoneType = telephonyManager.getPhoneType();
/**
* 獲取SIM卡提供的移動國家碼和移動網絡碼.5或6位的十進制數字.
* Returns the MCC+MNC (mobile country code + mobile network code) of the
* provider of the SIM. 5 or 6 decimal digits.
* <p>
* Availability: SIM state must be {@link #SIM_STATE_READY}
*SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
* @see #getSimState
*/
String simOperator = telephonyManager.getSimOperator();
/**
* 服務商名稱:
* Returns the Service Provider Name (SPN).
* <p>
* Availability: SIM state must be {@link #SIM_STATE_READY}
*SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
* @see #getSimState
*/
String simOperatorName = telephonyManager.getSimOperatorName();
/**
* Returns a constant indicating the state of the default SIM card.
*SIM的狀態信息:
* @see #SIM_STATE_UNKNOWN
* @see #SIM_STATE_ABSENT
* @see #SIM_STATE_PIN_REQUIRED
* @see #SIM_STATE_PUK_REQUIRED
* @see #SIM_STATE_NETWORK_LOCKED
* @see #SIM_STATE_READY
* @see #SIM_STATE_NOT_READY
* @see #SIM_STATE_PERM_DISABLED
* @see #SIM_STATE_CARD_IO_ERROR
*/
int simStat = telephonyManager.getSimState();
/**
* @return true if a ICC card is present
*/
boolean hasIcc = telephonyManager.hasIccCard();
/**
* 是否漫遊:
* (在GSM用途下)
* Availability: Only when user registered to a network.
*/
boolean isRoaming = telephonyManager.isNetworkRoaming();
/**
* 附近的電話的信息:
* 類型:List<NeighboringCellInfo>
* 需要權限:android.Manifest.permission#ACCESS_COARSE_UPDATES
*/
List<NeighboringCellInfo> list =
telephonyManager.getNeighboringCellInfo();//List<NeighboringCellInfo>
/**
* 獲取數據連接狀態
* @see #DATA_ACTIVITY_NONE
* @see #DATA_ACTIVITY_IN
* @see #DATA_ACTIVITY_OUT
* @see #DATA_ACTIVITY_INOUT
* @see #DATA_ACTIVITY_DORMANT
*/
int dataActivty = telephonyManager.getDataActivity();