TelephonyManager和PhoneStateListener

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取来通话状态。
主要成员函数
publicint getCallState()
Returns a constant indicating the call state (cellular) on the device.
取得手机的通话状态。
publicCellLocation 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.
publicint getDataActivity()
Returns a constant indicating the type of activity on a data connection (cellular).
返回当前 数据连接活动状态的情况。
publicint getDataState()
Returns a constant indicating the current data connection state (cellular).
返回当前 数据连接状态的情况。
publicString 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
publicString 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(手机号)
publicList<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中却找不到该常量。
publicString 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
int LISTEN_CALL_FORWARDING_INDICATOR Listen for changes to the call-forwarding indicator.
int LISTEN_CALL_STATE Listen for changes to the device call state.
int LISTEN_CELL_LOCATION Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener.
int LISTEN_DATA_ACTIVITY Listen for changes to the direction of data traffic on the data connection (cellular).
int LISTEN_DATA_CONNECTION_STATE Listen for changes to the data connection state (cellular).
int LISTEN_MESSAGE_WAITING_INDICATOR Listen for changes to the message-waiting indicator.
int LISTEN_NONE Stop listening for updates.
int LISTEN_SERVICE_STATE Listen for changes to the network service state (cellular).
int LISTEN_SIGNAL_STRENGTH This constant is deprecated. byLISTEN_SIGNAL_STRENGTHS
int LISTEN_SIGNAL_STRENGTHS Listen for changes to the network signal strengths (cellular).
PhoneStateListener的监听接口如下
Public Methods
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCallForwardingIndicatorChanged(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onCallForwardingIndicatorChanged</a></span>(boolean cfi)</nobr>
Callback invoked when the call-forwarding indicator changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCallStateChanged(int,%20java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onCallStateChanged</a></span>(int state,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>incomingNumber)</nobr>
Callback invoked when device call state changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCellLocationChanged(android.telephony.CellLocation)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onCellLocationChanged</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/telephony/CellLocation.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">CellLocation</a>location)</nobr>
Callback invoked when device cell location changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataActivity(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onDataActivity</a></span>(int direction)</nobr>
Callback invoked when data activity state changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataConnectionStateChanged(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onDataConnectionStateChanged</a></span>(int state)</nobr>
Callback invoked when connection state changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataConnectionStateChanged(int,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onDataConnectionStateChanged</a></span>(int state, int networkType)</nobr>
same as above, but with the network type.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onMessageWaitingIndicatorChanged(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onMessageWaitingIndicatorChanged</a></span>(boolean mwi)</nobr>
Callback invoked when the message-waiting indicator changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onServiceStateChanged(android.telephony.ServiceState)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onServiceStateChanged</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/telephony/ServiceState.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ServiceState</a>serviceState)</nobr>
Callback invoked when device service state changes.
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onSignalStrengthChanged(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onSignalStrengthChanged</a></span>(int asu)</nobr>
This method is deprecated. UseonSignalStrengthsChanged(SignalStrength)
<nobr style="line-height:21px">void</nobr> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onSignalStrengthsChanged(android.telephony.SignalStrength)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onSignalStrengthsChanged</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/telephony/SignalStrength.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SignalStrength</a>signalStrength)</nobr>
Callback invoked when network signal strengths changes.
结束!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值