Welcome 微信登录

首页 / 操作系统 / Linux / Android电话信息相关API

Android平台提供的电话信息系统管理功能,主要包括:获取电话信息(设备信息、SIM信息以及网络信息)、侦听电话状态(呼叫状态、服务状态、信号强度状态等)和调用电话拨号器。

TelephoneManager类

 在Manifest中添加Permission :<uses-permission android:name="android.permission.READ_PHONE_STATE" />TelephonyManager的获得:TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); 

Constants

StringACTION_PHONE_STATE_CHANGEDBroadcast intent action indicating that the call state (cellular) on the device has changed.
intCALL_STATE_IDLEDevice call state: No activity.空闲(无呼入或已挂机)
intCALL_STATE_OFFHOOKDevice call state: Off-hook.摘机(有呼入)
intCALL_STATE_RINGINGDevice call state: Ringing.响铃(接听中)
intDATA_ACTIVITY_DORMANTData connection is active, but physical link is down电话数据活动状态类型:睡眠模式(3.1版本)
intDATA_ACTIVITY_INData connection activity: Currently receiving IP PPP traffic.电话数据活动状态类型:数据流入
intDATA_ACTIVITY_INOUTData connection activity: Currently both sending and receiving IP PPP traffic.电话数据活动状态类型:数据交互
intDATA_ACTIVITY_NONEData connection activity: No traffic.电话数据活动状态类型:无数据流动
intDATA_ACTIVITY_OUTData connection activity: Currently sending IP PPP traffic.电话数据活动状态类型:数据流出
intDATA_CONNECTEDData connection state: Connected.数据连接状态类型:已连接
intDATA_CONNECTINGData connection state: Currently setting up a data connection.数据连接状态类型:正在连接
intDATA_DISCONNECTEDData connection state: Disconnected.数据连接状态类型:断开
intDATA_SUSPENDEDData connection state: Suspended.数据连接状态类型:已暂停
StringEXTRA_INCOMING_NUMBERThe lookup key used with the ACTION_PHONE_STATE_CHANGED broadcast for a String containing the incoming phone number.
StringEXTRA_STATEThe lookup key used with the ACTION_PHONE_STATE_CHANGED broadcast for a String containing the new call state.
intNETWORK_TYPE_1xRTTCurrent network is 1xRTT
intNETWORK_TYPE_CDMACurrent network is CDMA: Either IS95A or IS95B
intNETWORK_TYPE_EDGECurrent network is EDGE
intNETWORK_TYPE_EHRPDCurrent network is eHRPD
intNETWORK_TYPE_EVDO_0Current network is EVDO revision 0
intNETWORK_TYPE_EVDO_ACurrent network is EVDO revision A
intNETWORK_TYPE_EVDO_BCurrent network is EVDO revision B
intNETWORK_TYPE_GPRSCurrent network is GPRS
intNETWORK_TYPE_HSDPACurrent network is HSDPA
intNETWORK_TYPE_HSPACurrent network is HSPA
intNETWORK_TYPE_HSPAPCurrent network is HSPA+
intNETWORK_TYPE_HSUPACurrent network is HSUPA
intNETWORK_TYPE_IDENCurrent network is iDen
intNETWORK_TYPE_LTECurrent network is LTE
intNETWORK_TYPE_UMTSCurrent network is UMTS
intNETWORK_TYPE_UNKNOWNNetwork type is unknown
intPHONE_TYPE_CDMAPhone radio is CDMA.
intPHONE_TYPE_GSMPhone radio is GSM.
intPHONE_TYPE_NONENo phone radio.
intPHONE_TYPE_SIPPhone is via SIP.
intSIM_STATE_ABSENTSIM card state: no SIM card is available in the device
intSIM_STATE_NETWORK_LOCKEDSIM card state: Locked: requries a network PIN to unlock
intSIM_STATE_PIN_REQUIREDSIM card state: Locked: requires the user"s SIM PIN to unlock
intSIM_STATE_PUK_REQUIREDSIM card state: Locked: requires the user"s SIM PUK to unlock
intSIM_STATE_READYSIM card state: Ready
intSIM_STATE_UNKNOWNSIM card state: Unknown.
 

Fields

public static final StringEXTRA_STATE_IDLEValue used with EXTRA_STATE corresponding to CALL_STATE_IDLE.
public static final StringEXTRA_STATE_OFFHOOKValue used with EXTRA_STATE corresponding to CALL_STATE_OFFHOOK.
public static final StringEXTRA_STATE_RINGINGValue used with EXTRA_STATE corresponding to CALL_STATE_RINGING.
 

Public Methods

intgetCallState()Returns a constant indicating the call state (cellular) on the device.
CellLocationgetCellLocation()Returns the current location of the device.
intgetDataActivity()Returns a constant indicating the type of activity on a data connection (cellular).处理侦测到的数据活动的改变事件。通过该函数,可以获取数据活动状态信息。电话数据活动状态类型定义在TelephoneyManager类中。DATA_ACTIVITY_NONE            无数据流动
DATA_ACTIVITY_IN                    数据流入
DATA_ACTIVITY_OUT                数据流出
DATA_ACTIVITY_INOUT            数据交互
DATA_ACTIVITY_DORMANT     睡眠模式(2.1版本)
intgetDataState()Returns a constant indicating the current data connection state (cellular).
StringgetDeviceId()Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.获取设备标识(IMEI)
StringgetDeviceSoftwareVersion()Returns the software version number for the device, for example, the IMEI/SV for GSM phones.获得软件版本
StringgetLine1Number()Returns the phone number string for line 1, for example, the MSISDN for a GSM phone.线路1的电话号码
List<NeighboringCellInfo>getNeighboringCellInfo()Returns the neighboring cell information of the device.
StringgetNetworkCountryIso()Returns the ISO country code equivalent of the current registered operator"s MCC (Mobile Country Code).获取网络的国家ISO代码
StringgetNetworkOperator()Returns the numeric name (MCC+MNC) of current registered operator. 获取SIM移动国家代码(MCC)和移动网络代码(MNC)
StringgetNetworkOperatorName()Returns the alphabetic name of current registered operator. 获取服务提供商姓名(中国移动、中国联通等)
intgetNetworkType()Returns a constant indicating the radio technology (network type) currently in use on the device for data transmission.获取网络类型NETWORK_TYPE_UNKNOWN 未知网络
NETWORK_TYPE_GPRS   
NETWORK_TYPE_EDGE 通用分组无线服务(2.5G)
NETWORK_TYPE_UMTS    全球移动通信系统(3G)
NETWORK_TYPE_HSDPA 
NETWORK_TYPE_HSUPA
NETWORK_TYPE_HSPA
NETWORK_TYPE_CDMA         CDMA网络(2.1版本)
NETWORK_TYPE_EVDO_0     CDMA2000 EV-DO版本0(2.1版本)
NETWORK_TYPE_EVDO_A     CDMA2000 EV-DO版本A(2.1版本)
NETWORK_TYPE_EVDO_B     CDMA2000 EV-DO版本B(2.1版本)
NETWORK_TYPE_1xRTT         CDMA2000 1xRTT(2.1版本)
NETWORK_TYPE_IDEN
NETWORK_TYPE_LTE
NETWORK_TYPE_EHRPD
NETWORK_TYPE_HSPAP
intgetPhoneType()Returns a constant indicating the device phone type.电话类型 PHONE_TYPE_NONE   未知
PHONE_TYPE_GSM      GSM手机
PHONE_TYPE_CDMA   CDMA手机(2.1版本)
PHONE_TYPE_SIP         via SIP手机
StringgetSimCountryIso()Returns the ISO country code equivalent for the SIM provider"s country code.获取SIM卡中国家ISO代码
StringgetSimOperator()Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM.获得SIM卡中移动国家代码(MCC)和移动网络代码(MNC)
StringgetSimOperatorName()Returns the Service Provider Name (SPN).获取服务提供商姓名(中国移动、中国联通等)
StringgetSimSerialNumber()Returns the serial number of the SIM, if applicable.SIM卡序列号
intgetSimState()Returns a constant indicating the state of the device SIM card.SIM卡状态SIM_STATE_UNKNOWN                   未知状态
SIM_STATE_ABSENT                        未插卡
SIM_STATE_PIN_REQUIRED          需要PIN码,需要SIM卡PIN码解锁
SIM_STATE_PUK_REQUIRED        需要PUK码,需要SIM卡PUK码解锁
SIM_STATE_NETWORK_LOCKED    网络被锁定,需要网络PIN解锁
SIM_STATE_READY                          准备就绪其中:PIN个人识别码 PUK个人解锁码
StringgetSubscriberId()Returns the unique subscriber ID, for example, the IMSI for a GSM phone.获得客户标识(IMSI)
StringgetVoiceMailAlphaTag()Retrieves the alphabetic identifier associated with the voice mail number.
StringgetVoiceMailNumber()Returns the voice mail number.
booleanhasIccCard() 
booleanisNetworkRoaming()Returns true if the device is considered roaming on the current network, for GSM purposes.
voidlisten(PhoneStateListener listener, int events)Registers a listener object to receive notification of changes in specified telephony states.侦听电话的呼叫状态。电话管理服务接口支持的侦听类型在PhoneStateListener类中定义。
   

PhoneStateListener类

Constants

intLISTEN_CALL_FORWARDING_INDICATORListen for changes to the call-forwarding indicator.侦听呼叫转移指示器改变事件
intLISTEN_CALL_STATEListen for changes to the device call state.侦听呼叫状态改变事件
intLISTEN_CELL_LOCATIONListen for changes to the device"s cell location. Note that this will result in frequent callbacks to the listener.侦听设备位置改变事件
intLISTEN_DATA_ACTIVITYListen for changes to the direction of data traffic on the data connection (cellular).侦听数据连接的流向改变事件
intLISTEN_DATA_CONNECTION_STATEListen for changes to the data connection state (cellular).侦听数据连接状态改变事件
intLISTEN_MESSAGE_WAITING_INDICATORListen for changes to the message-waiting indicator.侦听消息等待指示器改变事件
intLISTEN_NONEStop listening for updates.停止侦听
intLISTEN_SERVICE_STATEListen for changes to the network service state (cellular).侦听网络服务状态
intLISTEN_SIGNAL_STRENGTHThis constant is deprecated. by LISTEN_SIGNAL_STRENGTHS 侦听网络信号强度
intLISTEN_SIGNAL_STRENGTHSListen for changes to the network signal strengths (cellular).
 

Public Constructors

 PhoneStateListener()          

Public Methods

voidonCallForwardingIndicatorChanged(boolean cfi)Callback invoked when the call-forwarding indicator changes.
voidonCallStateChanged(int state, String incomingNumber)Callback invoked when device call state changes.处理侦测到的电话呼叫状态的改变事件。通过该回调事件,可以获取来电号码,而且可以获取电话呼叫状态。即用switch(state){case TelephoneManager.CALL_STATE_……}来判断
voidonCellLocationChanged(CellLocation location)Callback invoked when device cell location changes.
voidonDataActivity(int direction)Callback invoked when data activity state changes.
voidonDataConnectionStateChanged(int state)Callback invoked when connection state changes.
voidonDataConnectionStateChanged(int state, int networkType)same as above, but with the network type.处理侦测到的数据连接状态的改变状态。通过该回调函数,可以获取数据连接状态信息。数据连接类型DATA_DISCONNECTED    断开
DATA_CONNECTING         正在连接
DATA_CONNECTED          已连接
DATA_SUSPENDED           已暂停
voidonMessageWaitingIndicatorChanged(boolean mwi)Callback invoked when the message-waiting indicator changes.
voidonServiceStateChanged(ServiceState serviceState)Callback invoked when device service state changes.处理侦测到的服务状态的改变事件。通过该回调函数可以获取服务状态信息。电话服务状态类型定义在ServiceState类中。
voidonSignalStrengthChanged(int asu)This method is deprecated. Use onSignalStrengthsChanged(SignalStrength) 处理侦测到的信号强度的改变事件。通过该回调函数,可以获取信号强度类型。
voidonSignalStrengthsChanged(SignalStrength signalStrength)Callback invoked when network signal strengths changes.
 

 电话拨号

  1. Uri uri=Uri.parse("tel:"+电话号码);  
  2. Intent intent=new Intent(Intent.ACTION_DIAL,uri);  
  3. startActivity(intent);  
其中的电话号码可以通过EditText中输入获得数据,如editText.getText().toString().trim()来获得
上面的代码可以放在Button的onClick方法中调用即可实现拨打电话的功能,调用的是系统的拨号界面。