您的位置:首页 > 理论基础 > 计算机网络

TelephonyManager和PhoneStateListener

2011-12-04 19:16 155 查看
public class
TelephonyManager
extends Object
java.lang.Object
    
android.telephony.TelephonyManager
TelephonyManager提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。
应用程序也可以注册listener来监听电话状态的改变。
我们不能对TelephonyManager进行实例化,只能通过如下的形式得到对其实例的引用:
Context.getSystemService(Context.TELEPHONY_SERVICE);
注意:对手机的某些信息进行读取是需要一定许可(permission)的。
可以通过在manifest file中添加一定的许可(permission),以便来对对手机的某些信息进行读取。
主要静态成员常量
手机的通话状态
它们对应PhoneStateListener.LISTEN_CALL_STATE所监听到的内容
int 
CALL_STATE_IDLE 
Device call state: No activity.
Device call state: No activity. 
空闲。没有任何活动。
int 
CALL_STATE_OFFHOOK 
Device call state: Off-hook.
Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting. 
摘机状态。至少有个电话活动。该活动或是拨打(dialing)或是通话,或是 on hold。并且没有电话是ringing or waiting
注:on hold的意思是暂时挂起。比如在进行通话A的时候,来了个新的电话B,我们暂时挂起通话A,等通话B结束了,才继续进行A的通话。
int 
CALL_STATE_RINGING 
Device call state: Ringing.
Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active. 
来电。来电,电话铃声响起的那段时间或正在通话又来新电,新来电话不得不等待的那段时间。
数据连接活动状态
它们对应PhoneStateListener.LISTEN_DATA_ACTIVITY所监听到的内容
int 
DATA_ACTIVITY_DORMANT 

Data connection is active, but physical link is down
int 
DATA_ACTIVITY_IN 

Data connection activity: Currently receiving IP PPP traffic.
int 
DATA_ACTIVITY_INOUT 

Data connection activity: Currently both sending and receiving IP PPP traffic.
int 
DATA_ACTIVITY_NONE 

Data connection activity: No traffic.
int 
DATA_ACTIVITY_OUT 

Data connection activity: Currently sending IP PPP traffic.
数据连接状态
它们对应PhoneStateListener.LISTEN_DATA_CONNECTION_STATE 所监听到的内容
int 
DATA_CONNECTED 

Data connection state: Connected.
int 
DATA_CONNECTING 

Data connection state: Currently setting up a data connection.
int 
DATA_DISCONNECTED 

Data connection state: Disconnected.
int 
DATA_SUSPENDED 

Data connection state: Suspended.
数据传送的网络类型
int 
NETWORK_TYPE_1xRTT 

Current network is 1xRTT
int 
NETWORK_TYPE_CDMA 

Current network is CDMA: Either IS95A or IS95B
int 
NETWORK_TYPE_EDGE 

Current network is EDGE
int 
NETWORK_TYPE_EHRPD 

Current network is eHRPD
int 
NETWORK_TYPE_EVDO_0 

Current network is EVDO revision 0
int 
NETWORK_TYPE_EVDO_A 

Current network is EVDO revision A
int 
NETWORK_TYPE_EVDO_B 

Current network is EVDO revision B
int 
NETWORK_TYPE_GPRS 

Current network is GPRS
int 
NETWORK_TYPE_HSDPA 

Current network is HSDPA
int 
NETWORK_TYPE_HSPA 

Current network is HSPA
int 
NETWORK_TYPE_HSUPA 

Current network is HSUPA
int 
NETWORK_TYPE_IDEN 

Current network is iDen
int 
NETWORK_TYPE_LTE 

Current network is LTE
int 
NETWORK_TYPE_UMTS 

Current network is UMTS 
int 
NETWORK_TYPE_UNKNOWN 

Network type is unknown
语音传送的网络类型
int 
PHONE_TYPE_CDMA 

Phone radio is CDMA.
int 
PHONE_TYPE_GSM 

Phone radio is GSM.
int 
PHONE_TYPE_NONE 

No phone radio.
int 
PHONE_TYPE_SIP 

Phone is via SIP.
SIM卡状态
int 
SIM_STATE_ABSENT 

SIM card state: no SIM card is available in the device
int 
SIM_STATE_NETWORK_LOCKED 

SIM card state: Locked: requries a network PIN to unlock
int 
SIM_STATE_PIN_REQUIRED 

SIM card state: Locked: requires the user's SIM PIN to unlock
int 
SIM_STATE_PUK_REQUIRED 

SIM card state: Locked: requires the user's SIM PUK to unlock
int 
SIM_STATE_READY 

SIM card state: Ready
int 
SIM_STATE_UNKNOWN 

SIM card state: Unknown.
手机通话状态在广播中的对应值
public static final String 
EXTRA_STATE_IDLE 

Value used with EXTRA_STATE corresponding to CALL_STATE_IDLE.
它在手机通话状态改变的广播中,用于表示CALL_STATE_IDLE状态
public static final String 
EXTRA_STATE_OFFHOOK 

Value used with EXTRA_STATE corresponding to CALL_STATE_OFFHOOK.
它在手机通话状态改变的广播中,用于表示CALL_STATE_OFFHOOK状态
public static final String 
EXTRA_STATE_RINGING 

Value used with EXTRA_STATE corresponding to CALL_STATE_RINGING.
它在手机通话状态改变的广播中,用于表示CALL_STATE_RINGING状态
String 
ACTION_PHONE_STATE_CHANGED 

Broadcast intent action indicating that the call state (cellular) on the device has changed.
The EXTRA_STATE extra indicates the new call state. If the new state is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming phone number as a String.
Requires the READ_PHONE_STATE permission.
This was a sticky broadcast in version 1.0, but it is no longer sticky. Instead, use getCallState() to synchronously query the current call state.
在广播中用ACTION_PHONE_STATE_CHANGED这个Action来标示通话状态改变的广播(intent)。
注:需要许可READ_PHONE_STATE。
String 
EXTRA_INCOMING_NUMBER  
The lookup key used with the ACTION_PHONE_STATE_CHANGED 
broadcast for a String containing the incoming phone number.
在手机通话状态改变的广播,用于从extra取来电号码。
String 
EXTRA_STATE  
The lookup key used with the ACTION_PHONE_STATE_CHANGED 
broadcast for a String containing the new call state.
在通话状态改变的广播,用于从extra取来通话状态。
主要成员函数
public int 
getCallState()
Returns a constant indicating the call state (cellular) on the device.
取得手机的通话状态。
public CellLocation getCellLocation ()
Returns the current location of the device. Return null if current location is not available.
返回手机当前所处的位置。如果当前定位服务不可用,则返回null
注:需要许可(Permission)ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity ()
Returns a constant indicating the type of activity on a data connection (cellular).
返回当前数据连接活动状态的情况。
public int getDataState ()
Returns a constant indicating the current data connection state (cellular).
返回当前数据连接状态的情况。
public String getDeviceId ()
Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available.
返回手机的设备ID。比如对于GSM的手机来说是IMEI码,对于CDMA的手机来说MEID码或ESN码。如果读取失败,则返回null。
注:需要许可(Permission)READ_PHONE_STATE
注:手机上的IMEI就是手机身份识别号码。IMEI码贴在手机背面的标志上,并且读写于手机内存中。它也是该手机在厂家的"档案"和"身份证号"。
关于IMEI的更多内容参考《手机设备身份识别码之IMEI
public String getDeviceSoftwareVersion ()
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.
返回手机软件版本号。比如对于GSM手机返回的是IMEI/SV码。如果读取失败,则返回null。
注1:需要许可(Permission)READ_PHONE_STATE
String getLine1Number ()
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
返回手机的Line1Number。比如对于GSM手机返回的是MSISDN码。如果读取失败,则返回null。
注1:需要许可(Permission)READ_PHONE_STATE
注2:关于MSISDN的更多内容请参考《MSISDN(手机号)
public List<NeighboringCellInfo> getNeighboringCellInfo ()
Returns the neighboring cell information of the device.
Returns
    * List of NeighboringCellInfo or null if info unavailable.
返回手机的neighboring cell信息
 注:文档上说需要许可(Permission)ACCESS_COARSE_UPDATES,但是在android.Manifest.permission中却找不到该常量。
public String getNetworkCountryIso ()
Returns the ISO country code equivalent of the current registered operator's MCC (Mobile Country Code).
返回网络所在的国家代码(ISO标准形式)
注1: 只有手机注册到了网络,该功能才可用. 
注2:如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
public String getNetworkOperator ()
Returns the numeric name (MCC+MNC) of current registered operator.
返回当前网络提供商的数字名字(MCC+MNC的形式)
如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
注:MCC(Mobile Country Code,移动国家号码),用于唯一地标识移动客户属于的国家。它由三位十进制数组成(000-999),例如我国的MCC为460
public String getNetworkOperatorName ()
Returns the alphabetic name of current registered operator.
返回当前网络提供商的名字(字母形式)
如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
public int getNetworkType ()
Returns a constant indicating the radio technology (network type) currently in use on the device for data transmission.
返回用于传输数据的网络的无线类型。比如GPRS
public int getPhoneType ()
Returns a constant indicating the device phone type. This indicates the type of radio used to transmit voice calls.
返回手机用于传输语言的无线类型。比如GSM,CDMA
public String getSimCountryIso ()
Returns the ISO country code equivalent for the SIM provider's country code.
返回SIM卡提供商的国家代码。
public String getSimOperator ()
Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM. 5 or 6 decimal digits.
返回SIM卡的提供商代码,以MCC+MNC的形式(mobile country code + mobile network code)。该码是5或6为的十进制形式。
注:只有SIM的状态是SIM_STATE_READY的时候,该功能才可用。
public String getSimOperatorName ()
Returns the Service Provider Name (SPN).
返回SIM卡服务商的名字。
注:只有SIM的状态是SIM_STATE_READY的时候,该功能才可用。
public String getSimSerialNumber ()
Returns the serial number of the SIM, if applicable. Return null if it is unavailable.
返回SIM卡的编码。如果读取失败,则返回null。
注:需要许可(Permission)READ_PHONE_STATE
public int getSimState ()
Returns a constant indicating the state of the device SIM card.
返回SIM卡的状态
public String getSubscriberId ()
Returns the unique subscriber ID, for example, the IMSI for a GSM phone. Return null if it is unavailable.
返回国际移动用户识别码。对于GSM手机来说是IMSI。如果读取失败,则返回null。
注1:imsi是国际移动用户识别码。它主要是用于国际漫游的。这个对用户来说应该是透明的。
关于imsi的更多内容可参考《国际移动用户识别码imsi
注2:需要许可(Permission)READ_PHONE_STATE
public String getVoiceMailAlphaTag ()
Retrieves the alphabetic identifier associated with the voice mail number.
使用语言邮箱号得到其字母形式的标识符(和语音邮件相关的标签)
注:需要许可(Permission)READ_PHONE_STATE
public String getVoiceMailNumber ()
Returns the voice mail number. Return null if it is unavailable.
得到语音邮箱码。
注:需要许可(Permission)READ_PHONE_STATE
public boolean hasIccCard ()
Returns
    * true if a ICC card is present 
    ICC卡是否存在
public boolean isNetworkRoaming ()
Returns true if the device is considered roaming on the current network, for GSM purposes.
查看是否在漫游。
注:它只在GSM下使用。
public void listen (PhoneStateListener listener, int events)
Registers a listener object to receive notification of changes in specified telephony states.
To register a listener, pass a PhoneStateListener and specify at least one telephony state of interest in the events argument. At registration, and when a specified telephony state
changes, the telephony manager invokes the appropriate callback method on the listener object and passes the current (udpated) values.
To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE (0).
参数
listener 
The PhoneStateListener object to register (or unregister)
events 
The telephony state(s) of interest to the listener, as a bitwise-OR combination of PhoneStateListener LISTEN_ flags. 
该参数为监听标志,以表示对什么进行监听,它为PhoneStateListener.LISTEN_CALL_STATE等的或运算值。
可以通过PhoneStateListener listener来监听手机的状态。
注1:unregister是通过把events设置为LISTEN_NONE (0)来实现的。
注2:PhoneStateListener除了可以监听手机的通话状态,数据连接活动状态,数据连接状态还可以监听很多状态。
注3:手机的通话状态可以通过Receiver来监听,具体见上面关于ACTION_PHONE_STATE_CHANGED的说明.
使用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).
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.
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.
voidonMessageWaitingIndicatorChanged(boolean
mwi)
Callback invoked when the message-waiting indicator changes.
voidonServiceStateChanged(ServiceState serviceState)
Callback invoked when device service state changes.
voidonSignalStrengthChanged(int
asu)
This method is deprecated. Use 
onSignalStrengthsChanged(SignalStrength)

voidonSignalStrengthsChanged(SignalStrength signalStrength)
Callback invoked when network signal strengths changes.
结束!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息